1
exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], function (opts, cb) {
    Account.upsert(opts, cb);
  }, callback);
};

Works fine, but

exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], Account.upsert, callback);
};

throws an error, saying that Account is undefined.

I would have thought that since Account.upsert takes the same arguments it would work. I've solved it for now, but I'm wondering what I'm missing here.

Jehan
  • 2,451
  • 2
  • 18
  • 27

1 Answers1

1

This doesn't work because the Account.upsert method uses the this value and JavaScript has dynamic this.

You can try binding it explicitly to the this value:

exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], Account.upsert.bind(Account), callback);
};
Community
  • 1
  • 1
Benjamin Gruenbaum
  • 246,787
  • 79
  • 474
  • 476
  • This issue is also noted in "Common Pitfalls" on their GitHub readme: https://github.com/caolan/async/blob/master/README.md#common-pitfalls – Qantas 94 Heavy Mar 01 '14 at 01:22
  • Oh cool, I didn't know that @Qantas94Heavy . I guess it's a common problem when using `async` if they have a whole section for it. – Benjamin Gruenbaum Mar 01 '14 at 01:23
  • Wow, thanks. Sorry for the dumb question. Hopefully it can save future developers from having to read the docs! – Jehan Mar 01 '14 at 02:15