質問

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.

役に立ちましたか?

解決

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);
};
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top