簡體   English   中英

多個查詢Node.js MySQL

[英]Multiple queries nodejs mysql

在這里運行多個查詢的代碼

 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。 節點的新手可以幫助我。 我嘗試用同步瀑布不了解。

您應該在第二個查詢之后而不是第一個查詢之后調用callback()函數。 根據文檔 ,該查詢可能甚至沒有被調用。

此外,您似乎正在覆蓋對象屬性,但是由於我不知道您的意圖,因此我不打算贅述。

我還建議您使用稍微更好的命名約定,即不要對兩個不同的查詢使用result1 ,從長遠來看,您最終會感到困惑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM