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' } ]
謝謝,這是我所需要的 –