1

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?

codebox
  • 19,927
  • 9
  • 63
  • 81

1 Answers1

2

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.

Danny Amey
  • 46
  • 1
  • That's what I would have assumed was happening, but the error message explicitly says that 'undefined has no method forEach' – codebox Dec 29 '13 at 11:52
  • 1
    @codebox: It says `HTTP Error undefined: undefined` has no method `forEach`. The stack trace format could be better. – Blender Dec 29 '13 at 11:52
  • 2
    Based on the twitter library that you're using the argument being passed to the callback is an error object, you can see the error construction on [line 97 of twitter.js](https://github.com/desmondmorris/node-twitter/blob/master/lib/twitter.js#L97). It's not using the normal Node `callback(err, data)` convention, so you'll need to check for errors using `if (trendData instanceof Error) { ... }`. – Danny Amey Dec 29 '13 at 12:09