Pergunta

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.

Foi útil?

Solução

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);
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top