2016-08-21 88 views
0

我是nodejs的新手,我試圖使用多個查詢來在單個頁面中顯示結果。我使用異步並行來獲得結果,但我無法處理請求。NodeJs處理多個mysql請求

這是我的回調函數

gettournamentDetail: function (res, req) { 
      var connection = mysqlConnectionProvider.getSqlConnection(); 

      //var test=[]; 
      var collection = { collection:[] }; 
      var sqlStatement = "SELECT * FROM tournaments WHERE tournamentId =" + res; 
      var tgroup1 = { collection:[]}; 

      var tournamentdetails = []; 
      var sqlStatement2 = "select * from " + 
       "(SELECT DISTINCT teamId, teamName,ttournament FROM teams, tournamentTeam WHERE ttournament=" + res + ") as main LEFT JOIN (SELECT DISTINCT groupName,groupTournament FROM groupTeam,tournamentGroup WHERE groupt=tgId) as sub ON sub.groupTournament=main.ttournament"; 
      // var return_data = {}; 
      async.parallel([ 
       function() { 

        if (connection) { 
         connection.query(sqlStatement, function (err, rows, fields) { 
          rows.forEach(function (row) { 
            tournamentdetails=row; 
           }); 

          req(tournamentdetails); 


        }); 


      } 


       }, 
       function() { 
        var tgroup=[]; 
        if (connection) { 
        connection.query(sqlStatement2, function (err, rows, fields) { 
         for (var i in rows) { 
          tgroup.push("group",{ 
           teamName: rows[i].teamName, 
           teamId: rows[i].teamId, 
           groupName: rows[i].groupName 
          }); 
         } 

          req(tgroup); 

        }); 
       } 

      } 

這是我的路由頁 exports.tournamentDetail =功能(REQ,RES){

VAR tournamentdetail =需要(」 ../數據庫/ getTournament。 JS');

tournamentdetail.tournament.gettournamentDetail(req.params.id,函數(集合){ 的console.log(集合); //res.render('tournamentdetail」,{標題: '錦標賽詳細',tdetail:收集});

}); };

任何想法如何處理這兩個請求來顯示數據。 這是我如何獲取數據

RowDataPacket { 
    tournamentId: 1, 
    tournamentUser: 1, 
    tournamentDate: 1472299200, 
    tournamentLocation: 'Reading', 
    tournamentDesc: 'An alternative to renaming app.js is to create an elastic beanstalk configuration file. Add a .config file into the .ebextensions folder, for example, .ebextensions/34.config. Change the NodeCommand setting in the namespace aws:elasticbeanstalk:container:nodejs to whatever command you want to run to start the server. For example, this is a minimal .config file to run npm start instead of app.js:\n\n', 
    tournamentInfo: 'Test', 
    tournamentCreated: 1471171131, 
    tournamentName: 'Ping Pong' } 
[ 'group', 
    { teamName: 'TeamName', teamId: 1, groupName: 'A' }, 
    'group', 
    { teamName: 'Team2', teamId: 2, groupName: 'A' } ] 

回答

1

我有點簡化你的代碼,但我不明白你需要什麼。

// connect on application start 
var connection = mysqlConnectionProvider.getSqlConnection(); 
if (!connection) 
    throw new Error('Smth wrong'); 
... 
let getTournamentDetail = function (tournament_id, callback) { 
    // I think both queries are bad, because they used Cartesian product and distinct 
    // Use placeholder to escaping params; it's more safety 
    var sql = 'select * from tournaments where tournamentId = ??'; 
    var sql2 = 'select distinct teamId, teamName, ttournament '+ 
     'from teams, tournamentTeam WHERE ttournament = ??) as main ' + 
     'left join (select distinct groupName, groupTournament ' + 
     'from groupTeam, tournamentGroup where groupt = tgId) as sub ' + 
     'on sub.groupTournament = main.ttournament"; 

    async.parallel([ 
     function (callback) { connection.query(sql, [tournament_id], callback) }, 
     function (callback) { connection.query(sql2, [tournament_id], callback) } 
     ], 
     function (err, results) { 
      if (err) 
       return callback(err); 

      callback(null, {tournamentdetails: results[0], tgrup: results[1]}) 
     } 
    ); 
} 
... 
var youRouteFunc = function (req, res) { 
    getTournamentDetail(req.params.id, function (err, data) { 
     if (err) 
      return res.send(err.message); 

     res.render('tournamentdetail', {title: 'Tournament Detail ', tdetail: data}); 
    }); 
} 
+0

謝謝,這是我所需要的 –