var mysql = require("mysql");
var thenJade = require('then-jade');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'nodejs'
});
exports.players_list = function(req, res) {
var data = {title: "", res: {}};
if (authenticate(req, res)) {
results_aaa(function(result) {
console.log(result);
res.render('players/players', {title: 'Players List', res: result});
});
} else {
req.session.error = 'Please login to continue.';
res.redirect('/login');
}
};
function results_aaa(callback) {
teams(function(res) {
callback(res)
});
}
function teams(callback) {
var response = [];
var query = connection.query("select * from team", function(err, result, fields) {
var response = [];
for (var index in result)
{
players(result[index].id, function(results) {
response.push(results);
});
}
console.log(response);
callback(response);
});
}
function players(id, callback) {
query("SELECT * FROM players where team = " + id, function(results) {
callback(results);
});
}
function query(sql, callback) {
connection.query(sql, function(error, results, fields) {
callback(results);
});
}
在这里,我需要根据各自的球队取得球员。但是在for循环中,我只得到第一个迭代值。因为对于异步行为,第二次和第三次迭代值是在数据发送到视图之后发生的。请帮忙
[ { id: 2, player_name: 'Virat Kohli', team: '1' },
{ id: 4, player_name: 'A B DeVilliers', team: '1' },
{ id: 6, player_name: 'Chris Gayle', team: '1' }
GET /players 200 177ms - 556
[ { id: 7, player_name: 'Ajinkya Rahane', team: '2' },
{ id: 8, player_name: 'Shane Watson', team: '2' },
{ id: 9, player_name: 'Stuart Binny', team: '2' },
{ id: 10, player_name: 'Karun Nair', team: '2' },
{ id: 11, player_name: 'Sanju Samson', team: '2' } ]
[ { id: 1, player_name: 'Virender Sehwag', team: '3' },
{ id: 3, player_name: 'David Miller', team: '3' },
{ id: 5, player_name: 'Shaun Marsh', team: '3' } ]
GET /css/bootstrap.min.css 304 5ms
GET /css/bootstrap.css 304 4ms
GET /css/style.css 304 4ms
如您所见,在第一次迭代后,将呈现视图。如何使用回调等待,直到forloop完成其执行,以便我将获得所有结果集。
请帮助
先谢谢
答案 0 :(得分:0)
试试这个
var async = require('async');
var mysql = require("mysql");
var thenJade = require('then-jade');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'nodejs'
});
exports.players_list = function(req, res) {
var data = {
title: "",
res: {}
};
if (authenticate(req, res)) {
results_aaa(function(result) {
console.log(result);
res.render('players/players', {
title: 'Players List',
res: result
});
});
} else {
req.session.error = 'Please login to continue.';
res.redirect('/login');
}
};
function results_aaa(callback) {
teams(function(err,res) {
callback(res);
});
}
function teams(callback) {
var response = [];
var query = connection.query("select * from team", function(err, result, fields) {
var tasks = [];
result.forEach(function(team) {
tasks.push(function(callback1) {
players(item.id, callback1);
});
});
async.parallel(tasks, function(error, response) {
callback(null, response);
});
});
}
function players(id, callback) {
query("SELECT * FROM players where team = " + id, function(error,results) {
callback(error,results);
});
}
function query(sql, callback) {
connection.query(sql, function(error, results, fields) {
callback(error,results);
});
}
答案 1 :(得分:0)
我知道你来自哪里回调。问题出现在'团队内部。功能。当您的“回复”时,系统会调用您的回叫循环仍在运行。你需要找到一种方法来执行所有“玩家”之后的回调。查询已完成。
function teams(callback) {
var response = [];
var query = connection.query("select * from team", function(err, result, fields) {
var response = [];
for (var index in result)
{
players(result[index].id, function(results) {
response.push(results);
});
}
console.log(response);
// The loop will continue the run... (because the queries take longer)
// the callback is executed BEFORE the loop is finished running
callback(response);
});
}
解决方案1: 看看promises。构造查询的方式阻止您使用回调来产生预期的结果。
解决方案2:
如果您希望各自的球队抓住所有球员,您只需要在球员牌桌上进行一次查询即可获得所需的输出。请查看下面的查询/示例(使用ORDER BY选择查询)。
var mysql = require("mysql");
var thenJade = require('then-jade');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'nodejs'
});
var getPlayersByTeam = function(callback) {
// New query
var playerQuery = "SELECT * FROM players ORDER BY team ASC";
connection.query(playerQuery, function(err, result, fields) {
callback(results);
});
};
var players_list = function(req,res){
var data = {
title: "",
res: {}
};
if (authenticate(req, res)) {
getPlayersByTeam(function(result){
res.render('players/players', {title: 'Players List', res: result});
});
} else {
req.session.error = 'Please login to continue.';
res.redirect('/login');
}
};
exports.players_list = players_list;