Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with forEach if iterator-function is prototype-function and uses "this" #176

Closed
jochenonline opened this issue Sep 12, 2012 · 2 comments

Comments

@jochenonline
Copy link

I am having problems using forEach if the iterator-function (2nd argument) is a prototype-function of an obejct and uses "this" within its function-body.

Concrete: I am trying to use forEach with "client.add" of node-solr (https://github.com/gsf/node-solr/blob/master/lib/solr.js - line 77).

When I use the following code:

async.forEachSeries( doc_arr, client.add, function(err) {
    if( err ) { 
         throw err; 
    }
});

I get: 'this' has no function 'update'. Which is in line 109 of the mentioned solr.js.

What can I do?

@fent
Copy link

fent commented Sep 12, 2012

This is how javascript works. If you give a function, it can get called within a different context.

You can specify the context with the Function#bind() method.

client.add.bind(client)

@jochenonline
Copy link
Author

..and this is how learning JS works: everyday something new. Thanks! It works!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants