Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What causes "no method" error when function invokes itself recursively?

What causes "no method" error when function invokes itself recursively?


I am writing a web crawler for reddit which only allows 1 request every 2 seconds and would like to use recursion to make the requests. After the series of get requests for 1 persons comments are complete, I would like to emit to show completion and call the comments again on the next username in a message queue. The problem is whenever I am more than one level deep I get a "TypeError: Object # has no method 'emit'".

I am also open for better ways to do this, I have just started learning node and am sure there are much better ways to accomplish this.

I have posted my code in the gist below...any help would be awesome!

Problem courtesy of: Hortinstein


The issue is with this which is a keyword (not free variable) that evaluates to the "receiver" for the function call.

For instance, given:

x = {fn: function () {return this} }

Then inside fn, this evaluates to what x evaluated to (and thus x.fn() === x). In the code grabComments is being invoked with no receiver, in which case this reverts to the global object. You may be interested in, or just simplify the code to handle recursion with a nested function so that self can remain bound-to in scope.

(In Javascript, methods are not bound to objects: they are merely functions shoved into properties, so the receiver is paramount in determining this for the executiong function.)

See also:

  • jQuery/JavaScript "this" pointer confusion
  • In Javascript, why is the "this" operator inconsistent?
  • Javascript "this" variable confusion
  • How do JavaScript closures work?

Happy coding.

Solution courtesy of: user166390


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What causes "no method" error when function invokes itself recursively?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription