CNode

请教mongoose查询中回调函数怎样能拿到查询参数的值?

Jjohnsmith0602发布于13 年前最后回复13 年前1 回复4699 浏览0 收藏

我需要在一个for循环中进行数据库查询,如果没有匹配把对应的index存进一个数组。

for ( var index = 0; index < 100; index++) {
    mongooseModel.findOne({ id: index }, function (err, model) {
        if (!model) {
            array.push(index);
        }
    });
}

由于mongoose的回调函数是异步的,我这么写存的index值是有问题的。有什么办法能把查询参数传给回调函数呢?

查看回复

回复 (1)

B
blacktea#1·13 年前

试一下async模块:https://github.com/caolan/async

参与回复
登录后即可参与回复。登录