In this piece of Javascript
creds = cp[0];
data[creds.toString()] = cp[1]; // data is an object
Chrome gives me the error TypeError: Cannot call method toString of undefined on the second line. However, I've verified via the debugger that the value of creds is at that point the number 1400.
What's going on?
You should be very cautious when using for in loop to array. Use normal loop instead.
The array cpl has not just data but functions, so third cp in the loop is function. That's why creds turned to undefined.
This link has good explanation: Why is using "for...in" with array iteration a bad idea?
Javascript don't need variable type so do it by removing toString().
btw I'm not sure you can call toString() on a primitive type as int
Related
Recently, I met a trouble with RegExp.prototype. As normal, RegExp.prototype is an empty regular expression, but, I try to use RegExp.prototype.test('') in console , it will be wrong. Why it will show error
console.log(RegExp.prototype.test.toString());
//RegExp.prototype.test is a native function
//if you want call this function,you should new a Regexp
console.log(new RegExp(/^$/).test(''));
I have a Javascript object that gets initialised in an asynchronous manner, later edited and then stored in an array until it can be saved. The problem is that one of the checks it goes through is _.has(obj, 'Id'). This test always fails. I've used JSON.stringify() to log the object's internals and everything has its value and looks correct. When I run a simple test of this line with hard-coded values in plunker, everything works as expected. I've tried accessing the Id values through obj.Id and obj['Id'] but these both return undefined and I don't know why. Why can stringify read the property but a regular accessor can't? The Id is a value that is not touched between initialisation and saving of the object.
My node application just spat out this error, which I don't understand:
trendData && trendData.forEach(function(trendDataItem){
^
TypeError: Object Error: HTTP Error undefined: undefined has no method 'forEach'
at /home/myapp/node/dataSource/enabled/twitterTrending.js:9:36
The && operator should be preventing any undefined values of trendData reaching the second part of the expression, but it seems not to have worked. My understanding of node.js is that my code doesn't have to concern itself with multi-threading issues, so I assume there's no chance that the value of trendData changed between the evaluation of the first part of the expression and the second.
The trendData value is passed in from a callback made by a twitter library, source code here, the line that is failing is inside the 'callback' function, from what I can see it should be the result of JSON.parse if everything worked, otherwise it will contain failure information.
Anybody have a clue what's going on?
Your understanding is correct, which means in this case trendData isn't undefined or some other falsy value, but instead is an object which doesn't have a forEach method. Based on the limited snippet provided trendData is a string containing an error message rather than the array you are expecting.
In a recent day I faced with this kind of error message, that has no position specified. Debugging becomes much more difficult. What did I do wrong?
When I started re-define JSON.parse functions of my objects, after that appears it first time.
I replaced them to normal functions, but the error message is still strange.
Maybe because I'm writing prototype functions, and I call them before constructing a new object?
Is it because I call a function from console?
I managed to simplify the error:
var o = {g : false};
function f(){
console.log(o.g());
}
If I call it from Chrome and form console, than I will not get any line number.
I recently had an issue with some javascript that goes against every bone of my programming background. Javascript does this often to me, so I'm not that surprised.
I have a function as such...
function x(param1, booleanParam, arrayParam){
....
}
I was getting a runtime error saying that arrayParam.length was not defined. On debugging I saw this was true and went to find out why. Turns out I had forgotten a comma in my function call as such...
x(param1, true [arrayJunk]);
The problem I'm having is figuring out why this call was made at all? Why isn't this a compile error, how does Javascript see this and think, "Yeah, that seems like it might work!"
Thanks in advance for any enlightenment you can share!
That's an indexing expression.
It's the same syntax as someArray[someIndex].
It will end up passing undefined as the second parameter too, unless arrayJunk happens to be the name of a property of boolean primitives.
What happens is the following:
JavaScript engine converts true into a Boolean object (not the primitive)
It then tries to access the property name stored in arrayParam from that object
Property doesn't exist, so it returns undefined
If arrayParam was the string "toString", it would return a function object
In this case the expression was being interpreted as an index. Essentially the same as
someArray[42]
So it was being seen as a function call with 2 parameters instead of 3
Many dynamic languages don't check if you pass too many or too few arguments to a function.
While this can sometimes mask errors, it also allows you to roll your own defalut parameter scheme.