Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点JS、express、Mongoose、嵌套查询_Javascript_Mongodb_Node.js_Express - Fatal编程技术网

Javascript 节点JS、express、Mongoose、嵌套查询

Javascript 节点JS、express、Mongoose、嵌套查询,javascript,mongodb,node.js,express,Javascript,Mongodb,Node.js,Express,我有一组带有express/mongoose的嵌套查询,很像这样: app.get(..., function(...) { Schema1.query(..., function(..., res1) { for ( var key in res1 ) { Schema2.query(..., function(..., res2) { data[key].appendedAttribute = res2.somedata;

我有一组带有express/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);
   });
}))

它不起作用,也就是说,appendedAttribute永远不会附加到数据集。我做错了什么?

使用

基本上,您只能在第二次查询完成后启动res.render调用。

使用:


异步编程不是这样工作的,您不认为只有在执行所有查询之后才调用cb()。。?(因此只有一次)@alesioalex
cb
必须调用
n
次。在它继续之前。n是之后的第一个参数。i、 e.他提出的金额。
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();
            });
        }
    });
});
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);
    }
  );
});