我的 Express Controller 中有这样的代码
function (req, res) {
var queries = [];
data.forEach(function (item) {
var query = myModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true}).exec;
queries.push(query);
});
async.parallel(queries, function (err, docs) {
res.json(docs);
});
});
如果数据数组有 3 个项目,那么我有一个包含 3 个空值的数组。
async.parallel
函数接受带有回调参数的函数,应该调用该函数来正确完成其执行。所以 mongoose.Query.exec 的作用是相同的。但结果我收到了一个空对象数组。
如果我像这样包装我的 exec
调用
var query = function (cb) {
tournamentsModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true}).exec(function (err, model) {
cb(err, model);
});
};
queries.push(query);
一切都很好,结果我从 mongo 收到了 3 个文档。
当 exec
方法执行相同操作时,为什么我应该显式调用传递给 async.parallel
函数调用的回调?
最佳答案
当您直接将查询的 exec
函数作为要执行的函数传递给 async.parallel
时,您将丢失包含要运行的查询的该函数调用的 this
上下文。
要使用此方法,您需要调用 bind
返回一个新函数,该函数将使用正确的 this
上下文调用 exec
;所以像这样:
var query = Query.prototype.exec.bind(
myModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true})
);
queries.push(query);
自己调用 exec
可能更干净,但将 async
回调传递给它:
var query = function(cb) {
myModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true}).exec(cb);
}
queries.push(query);
关于node.js - mongoose exec 调用 async.parallel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28583952/