charAt coming back as undefined - javascript

I've got the following function, however it's saying the charAt is undefined. The error is relating to the alert line. If i do alert(value) it gives me the value no problems.
$scope.markAnswer = function(answerID, questionID) {
if ($scope.containsObject(answerID, $scope.selectedAnswer)) {
$scope.selectedAnswer.splice($scope.selectedAnswer.indexOf(answerID), 1);
} else {
$scope.selectedAnswer.push(answerID);
}
angular.forEach($scope.selectedAnswer, function(value, key) {
alert(value.charAt(0));
if(questionID == res){
$log.info("questionID");
}
});
}
The following error:
TypeError: undefined is not a function
at http://127.0.0.1:9000/modules/core/controllers/home.js:57:25
at Object.forEach (http://127.0.0.1:9000/lib/angular/angular.js:325:18)
at Scope.$scope.markAnswer (http://127.0.0.1:9000/modules/core/controllers/home.js:56:17)
at http://127.0.0.1:9000/lib/angular/angular.js:10903:21
at http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:441:9
at Scope.$eval (http://127.0.0.1:9000/lib/angular/angular.js:12811:28)
at Scope.$apply (http://127.0.0.1:9000/lib/angular/angular.js:12909:23)
at HTMLDivElement.<anonymous> (http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:440:13)
at HTMLDivElement.jQuery.event.dispatch (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4430:9)
at HTMLDivElement.elemData.handle (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4116:28)

You could try this one:
String(value).charAt(0)
I suppose that this is happening because your value is not a string and charAt is a method that returns the character at the specified index in a string.

Related

How can i proxy a mediastream object without TypeError: Illegal invocation

I recently was trying to proxy a MediaStream Object in javascript And noticed i could but there was an error. I used the following code.
function createChangeableStream(stream) {
stream["_stream_"]=stream;
return new Proxy(stream,{
set: function(obj, prop, value){
if(prop==="_stream_")
obj._stream_=value;
else
obj._stream_[prop]=value;
},
get: function(obj, prop) {
if(prop==="_stream_")
return obj._stream_;
else
return obj._stream_[prop];
}
});
}
since i could not change the target... i used the above workaround.
Each time i try to call MediaStream.getTracks(), i get TypeError: Illegal invocation I would be grateful for your help. The function works with any other objects. and the 'target' is changed by modifying _stream_ property. I also realized that this
let x=new Proxy(stream,{});
x.getTracks()
doesn't work.
Okay for anyone with almost similar problem i succeeded evading the error with the following code.
function createChangeableStream(stream) {
stream["_stream_"]=stream;
stream["_call_handler_"]=function (obj,prop,args) {
return obj[prop](...args);
}
return new Proxy(stream,{
set: function(obj, prop, value){
if(prop==="_stream_")
obj._stream_=value;
else
obj._stream_[prop]=value;
},
get: function(obj, prop) {
if(prop==="_stream_")
return obj._stream_;
else if((typeof obj._stream_[prop])==="function"){
return function () {
return obj._call_handler_(obj._stream_,prop,[...arguments]);
}
}else
return obj._stream_[prop];
}
});
}
but i still don't know why the error exists at the first place. maybe its a bug, i don't know.

Uncaught TypeError: Cannot read property 'toString' of null

I have this message: "Uncaught TypeError: Cannot read property 'toString' of null" while running the code below. Any help would be appreciated since I am not familiar with Javascript
Thank you in advance.
function getUrlVars() {
var arrGamePath = document.referrer;
if(arrGamePath) {
var reg = new RegExp("[/][a-zA-Z]{2}-[a-zA-Z]{2,4}[/]");
var locale = reg.exec(arrGamePath) .toString();
while (locale.indexOf("/") != -1) {
locale = locale.replace("/", "");
}
return locale;
}else{
return false;
}
}
if(getUrlVars()) {
sCID = getUrlVars();
}
Your regex doesn't match and so returns null. null doesn't have a method toString(). So you should check for a match first and if it doesn't match, return false (or do whatever else you want to do - or change your regex so that it matches)
function getUrlVars() {
var arrGamePath = document.referrer;
if(arrGamePath) {
var reg = new RegExp("[/][a-zA-Z]{2}-[a-zA-Z]{2,4}[/]");
var matches = reg.exec(arrGamePath);
if (!matches) return false;
var locale = matches.toString();
while (locale.indexOf("/") != -1) {
locale = locale.replace("/", "");
}
return locale;
}else{
return false;
}
}
if(typeof getUrlVars == 'function') {
sCID = getUrlVars();
}
Also you are calling the function twice, once in your if line, ignoring the result:
if (getUrlVars())
and then if the if returns true, again. Just check if its type is a function instead.
So this error is related to the null property which while you did not declare a null property, that is what you are getting back as #baao shared in the first answer.
So whenever you try to access a property under a value of null or undefined in JavaScript and try to read any property, call any function on it, you get an error message: cannot read properties of null, reading toString(), so that’s what’s going on behind the scenes that’s why you are getting an error.
You are trying to call toString() on matches which is currently null or was null.
In the modern world of TypeScript you could probably solve that like so:
const reg: null || string = new RegExp("[/][a-zA-Z]{2}-[a-zA-Z]{2,4}[/]");
And that might indicate, hey this reg variable is either a string or it might be null.

Javascript/Underscore won't skip undefined result

I am trying to skip a line of code if there is nothing to do. However, I receive the error: TypeError: _.pairs(...)[0] is undefined. Why do I still receive this error? the function conditionalFilter is supposed to skip if it is undefined
Code:
conditionalFilter(_.pairs(_.pairs(_.pairs(d.nodes[0].children)[0][1].children)[0][1].children)[0][1].dimension, d.dimension.name, d.name)
Function:
function conditionalFilter(check, dim, filter){
if (check != "undefined") {
myFunction(check, dim, filter);
} else {}
}
If it makes a difference, the error throws on the line conditionalFilter(_.pairs...)
You need to take the quotes off of undefined,
the way you have it now you are checking it as a string:
function conditionalFilter(check, dim, filter){
if (check != undefined) {
myFunction(check, dim, filter);
} else {}
}

How to fix the following errors

I implemented spectrum color picker, and I'm trying to fix up the JSLint errors. I have 2 types of errors which I can't seem to fix. Here are the errors:
Unexpected '~'
Unexpected 'in'. Compare with undefined, or use the hasOwnProperty method instead.
Here's the code for the first error:
function contains(str, substr) {
return !!~('' + str).indexOf(substr);
}
Code for second error:
var hasTouch = ('ontouchstart' in window);
function contains(str, substr) {
return str.indexOf(substr) !== -1;
}
var hasTouch = window.ontouchstart !== undefined;

JS exports var, if it has no value I get no method exists, if it has value its ok

I need to access and array from another file in my nodejs/socket.io project(not relevant to this question however), index.js:
exports.rooms = function(){
if(username < usernames.length){
return usernames;
}
return false;
}
In admin.js
I use:
var index = require('./index.js');
The problem is if the exports.rooms function has no values I get the following error:
Missing error handler on `socket`.
TypeError: Object #<Object> has no method 'rooms'
Now the function is there it just has no values... I have tried the following function but i get the same error:
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
Thanks

Categories