我是新来的NodeJS,我不正确地理解如何运作异步工作异步功能。我今天读了很多关于他们的文章,但我无法解决我的问题。如何妥善处理的NodeJS
我用Sequelize.js作为ORM,我的问题是,当我巢查询到其他查询的回调,然后我不能强迫只有当查询结束它继续。
这里是我当前的代码:
io.on('connection', function (socket) {
socket.on('join', function (data) {
clients[clients.length] = new Client("Client " + clients.length, data.channel);
console.log('Client connected Channel: ' + clients[clients.length-1].channel);
var array = []
DB.Matches.findAll({attributes: ['matchId', 'teamAId', 'teamBId']}).then(function (result) {
for (var i = result.length - 1; i >= 0; i--) {
DB.Teams.findAll({where: { team_id: [result[i].teamAId,result[i].teamBId]}}).then(function (teams) {
array.push({ id: 0, name: teams[0].clubName + ' - ' + teams[1].clubName});
}).then(function() {
// Now my emit event is here but I dont want to run every time the loop run
console.log(array);
socket.emit('matches', array);
});
}
}.then(function() {
// I tried to put it here, but then I got an empty array, because the queries haven't finshed yet
}));
});
});
当该代码被调用时,阵列将在每一个循环在一个更多元的每一个回路可以emited,但这不是为我好。当数组完全填充时,我想调用emit事件一次。
[标签:sequelize.js]不具有'DB.Matches.findAll({属性:[ 'matchId', 'teamAId', 'teamBId']})'方法。请阅读文档[here](http://docs.sequelizejs.com)。 –
DB.Matches和DB.Teams是必需的()模式等: 'module.exports =函数(sequelize,Sequelize){ 变种模块= {}; 模块= sequelize.define( 'tbl_team',{ blablabla });返回模块; };' – NoNameProvided