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 {}
}
Related
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.
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;
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.
I need to check if thevar[2] === 'debug' however thevar[2] might be undefined so if I ran the following code with it being undefined javascript would throw an error:
if (thevar[2] === 'debug') {
console.log('yes');
}
So what I'm currently doing is:
if (typeof thevar[2] !== 'undefined') {
if (thevar[2] === 'debug') {
console.log('yes');
}
}
Is this really the best way to do this?
Your first example will not throw an error. Undefined properties of objects evaluate to undefined, but they don't throw errors.
var foo = {};
var nothing = foo.bar; // foo.bar is undefined, so "nothing" is undefined.
// no errors.
foo = [];
nothing = foo[42]; // undefined again
// still no errors
So, your second example is not needed. The first is sufficient.
If you can run if (typeof thevar[2] !== 'undefined') ... then you can reference thevar and you can run anything else with it.
If your array exists then checking against a value works fine, even if that value is undefined.
> var x = [];
undefined
> if ( x[0] === "debug" ) console.log("yes");
undefined
> if ( x[100] === "debug" ) console.log("yes");
undefined
The issue arises only when the array doesn't already exist. So as long as you know thevar has value then no check needed. Otherwise just check if thevar has value or do a little var assignment trick like
var thevar = thevar || [];
//work with thevar with impunity
function valid()
{
begin_checked = false;
end_checked = false;
alert("begin_checked: " +begin_checked);
alert("end_checked: " +end_checked);
if (document.dd.begin.checked.length == undefined || document.dd.end.checked.length == undefined )
{
alert("In undefined");
}
alert("end");
}
When the if statement is false, it never gets to alert("end") ? When it is true, it executes properly. Why?
There is probably a null pointer exception and you do not have errors outputting to your browser.
You need some output to check:
alert(document);
alert(document.dd);
alert(document.dd.begin);
alert(document.dd.begin.checked);
alert(document.dd.end);
alert(document.dd.end.checked);
If you get undefined from any of those, then your code will not execute properly.
Edit: Also, the other answers here all have good information. Read those as well.
Edit2: Alternative - Surround your code in a try/catch block and alert the error:
function valid(){
try{
begin_checked = false;
end_checked = false;
alert("begin_checked: " +begin_checked);
alert("end_checked: " +end_checked);
if (document.dd.begin.checked.length == undefined || document.dd.end.checked.length == undefined ){
alert("In undefined");
}
alert("end");
} catch (e) {
alert(e);
}
}
Are there any errors in your browsers error console? I'm guessing it's because it's trying to evaluate a property that doesn't exist, this causing it to fail (never getting to the == undefined). You can just check that the property exists or use the typeof to check if it's undefined.
if (!document.dd.begin.checked.length || !document.dd.end.checked.length)
{
alert("In undefined");
}
if (typeof document.dd.begin.checked.length == 'undefined' || typeof document.dd.end.checked.length == 'undefined' )
{
alert("In undefined");
}
http://getfirebug.com/