node.js - 带有异步库的循环内的 Mongoose 异步调用

标签 node.js mongodb mongoose mongoose-schema async.js

我刚开始使用 nodejs/mongoose,我想我有一个典型的异步问题。有人可以指导我如何解决这个异步问题吗?

我有这个函数“getAreasRoot”,在里面我有一个循环来用另一个异步函数的结果填充子项。如何使用异步库修复它?

areaSchema.statics.getAreasRoot = function(cb: any) {
    let self = this;
    return self.model("Area").find({ parentId: null }, function(err: any, docs: any){
        docs.forEach(function(doc: any){
            doc.name = "Hi " + doc.name;
            doc.children = self.model("Area").getAreasChildren(doc._id, function(err: any, docs: any){});
        })
        cb(err, docs);
    });
};

areaSchema.statics.getAreasChildren = function(id: any, cb: any) {
    return this.model("Area").find({ parentId: null }).exec(cb);
}

最佳答案

您有 2 个任务:获取根,然后让 children 使用根。

如果我要使用 async.js 执行此操作,我会使用 async.waterfall 的组合和 async.mapSeries .我们使用 .waterfall 是因为我们想将第一个任务的结果传递给第二个任务。我们使用 .mapSeries 因为我们想改变每个根区域的名称和子区域。

areaSchema.statics.getAreasRoot = function (cb) {
    let self = this;
    async.waterfall([
        // every task has a callback that must be fired at least/most once
        // to tell that the task has finished OR when an error has occurred
        function getRoots (cb1) {
            self.find({ parentId: null }, cb1);
        },
        function getChildren (roots, cb2) {
            async.mapSeries(roots, function (root, cb3) {
                // inside this block, we want to fire the innest callback cb3 when 
                // each iteration is done OR when an error occurs to stop .mapSeries
                self.find({ parentId: root._id }, function (err, children) {
                    // error: stop .mapSeries
                    if (err)
                        return cb3(err);
                    root.name = "Hi " + root.name;
                    root.children = children;
                    // done: send back the altered document
                    cb3(null, root);
                });
            // the last argument is fired when .mapSeries has finished its iterations
            // OR when an error has occurred; we simply pass the inner callback cb2
            }, cb2) 
        }
    // the last argument is fired when .waterfall has finished its tasks
    // OR when an error has occurred; we simply pass the original callback cb
    ], cb);
};

使用它

Area.getAreasRoot(function (err, areas) {
    console.log(err, areas);
})

放在一边

Mongoose 操作是异步的,所以

doc.children = self.model("Area").getAreasChildren(...) 

不正确,因为您返回的是与实际文档相反的 Promise。

还有

可能可以使用virtual population 来简化您的逻辑或 aggregation .

关于node.js - 带有异步库的循环内的 Mongoose 异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461176/

相关文章:

node.js - 如何在express服务器中进行API调用

node.js - 如何通过url在node js中渲染日志/文本文件?

javascript - 在 javascript 中,我应该尽可能使用 const 而不是 var 吗?

python - 减少从 MongoDB 加载 Pandas 数据帧所用的内存

javascript - MongoDB $in 和 $ne

mongodb - 无法解决 MongoParseError : Invalid connection string

mongodb - 如何在 Mongoose 中使用 Document#update?

node.js - 蒙哥错误: must have $meta projection for all $meta sort keys using Mongo DB Native NodeJS Driver

javascript - 更新 Meteor 上的 mongodb 集合

node.js - 在 Mongoose 模式上应用 2dsphere 索引是否会强制要求位置字段?