我剛開始使用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);
}
可能的重複[如何在for循環中使用貓鼬函數?](https://stackoverflow.com/questions/44569770/how-can-i-use-mongoose-functions-inside-a-for-循環) –
@KevinB如果OP詢問如何將它與async.js一起使用,那麼它並不是真的重複。沒有提供的答案使用async.js。 – Mikey