【发布时间】:2018-03-09 17:26:45
【问题描述】:
我刚开始使用 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);
}
【问题讨论】:
-
@KevinB 如果 OP 询问如何将其与 async.js 一起使用,则不是真正的重复。提供的答案都没有使用 async.js。
标签: node.js mongodb mongoose mongoose-schema async.js