Node JS,express,Mongoose,嵌套查询

我有一组嵌套查询与快速/mongoose,非常像这样:

app.get(..., function(...) { Schema1.query(..., function(..., res1) { for ( var key in res1 ) { Schema2.query(..., function(..., res2) { data[key].appendedAttribute = res2.somedata; }); } res.render(..., data); }); 

});

哪个不起作用,即,appendAttribute不会附加到数据集。 我究竟做错了什么?

after使用

 app.get(..., function(...) { Schema1.query(..., function(..., res1) { var cb = after(Object.keys(res1).length, function () { res.render(..., data); }); for (var key in res1) { Schema2.query(..., function(..., res2) { data[key].appendedAttribute = res2.somedata; cb(); }); } }); }); 

基本上你只有在第二个查询完成后才能触发res.render调用。

使用步骤 :

 app.get(..., function(...) { var data; Step( function first_query() { Schema1.query(...,this); }, function multiple_queries(err, res1) { for (var key in res1) { Schema2.query(..., function(..., res2) { data[key].appendedAttribute = res2.somedata; this.parallel(); // make sure callback gets executed only after all the queries are executed }); } }, function render() { res.render(..., data); } ); });