多个查询nodejs mysql

问题描述 投票:0回答:1

在这里运行多个查询是代码

 module.exports.getAllternaryCat=function(req,res) {


     var output = {};

  connection.query('SELECT * FROM ternary_categories',function(error,results,filelds){
      if(error) throw err;

      async.eachSeries(results,function(data,callback){ // It will be executed one by one
              //Here it will be wait query execute. It will work like synchronous
            //  output.push(data);
                  output['ternaryCategory']= results;
              connection.query('SELECT * FROM secondary_category  where id = '+data.secondary_categoryId,function(error,results1,filelds){
                  if(error) throw err;

                //  output.push(results1)
                output['secondaryCategory']= results1;


                  callback();
              });

              connection.query('SELECT * FROM primary_category  where id = '+data.secondary_categoryId,function(error,results1,filelds){
                  if(error) throw err;

                output['PrimaryCategory']= results1;


              });


      }, function(err, results) {
           res.json({
                    status:true,
                    data:output

                })
      });


  })

}

主类别的查询不返回结果对象,只返回ternaryCategory,secondaryCategory,而不返回PrimaryCategory。节点新手帮我解决这个问题。我尝试用同步水落不明白。

javascript node.js async.js
1个回答
0
投票

您应该在第二次查询后调用callback()函数,而不是在第一次查询之后。根据documentation,该查询可能甚至没有被调用。

此外,你似乎正在覆盖你的对象属性,但我不打算进入那个,因为我不知道你的意图,只是一个暗示。

我还建议你使用一个更好的命名约定,即不要将results1用于两个不同的查询,从长远来看,你最终会让自己感到困惑。

© www.soinside.com 2019 - 2024. All rights reserved.