【发布时间】:2016-02-06 19:49:26
【问题描述】:
我必须在 for 循环中重复运行 mongoose 查询,一旦完成,我想以 express 呈现页面。下面给出的示例代码。由于 mongoose 是异步运行的,我怎样才能让“commands/new”页面仅在 for 循环中填充“commands”数组后才呈现?
...
...
var commands = [];
for (var index=0; index<ids.length; index++) {
mongoose.model('Command').find({_id : ids[index]}, function (err, command){
// do some biz logic with the 'command' object
// and add it to the 'commands' array
commands[index] = command;
});
}
res.render('commands/new', {
commands : commands
});
...
...
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query