如何正确调试 Sails.js 中的响应生命周期?
How to properly debug the response lifecyle in Sails.js?
使用 sails.js 我得到了这些执行数据库请求和使用承诺的控制器方法。问题是它们正在工作,但是当执行请求时,客户端会收到 504。
我想确保我的实现是好的,并且 504 在 sails/node 编码中不是问题,而是任何 nginx 或其他东西。我在 config/logs.js
中将日志模式设置为愚蠢,但每次调用 response.send
时我什至看不到。
下面的两种方法都存在这样的问题,但(也许是巧合)第一种方法只是有时会发生。
方法一
addPlayer: function (request,response) {
console.log("Add player");
var lineupId = request.params.id;
var receivedPlayer = request.param('player');
var playerId = receivedPlayer.id;
var bench = receivedPlayer.bench;
var place = receivedPlayer.place;
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
var player = Player.findOne().where({id: playerId}).then(function (player) {
console.log("Found player with _id " + player.id);
return player;
});
return [foundLineUp,player];
}).spread(function (lineup,player) {
if (!lineup)
return response.send(404);
if (!player)
return response.send(409, "El jugador " + playerId + " no existe");
else if (!lineup.formation)
return response.send(409, "No se ha elegido una táctica para esta alineación");
if (lineup.squadIsComplete())
return response.send(409, "La plantilla ya contiene el máximo de 15 jugadores");
if (lineup.playerWasAdded(player.id))
return response.send(409, "El jugador ya ha sido agregado a la alineación");
if (lineup.fieldIsComplete() && !bench)
response.send(409, "Ya se han agregado los 11 jugadores de campo");
/*If there's already a player with same position, return 409, should modify*/
player.bench = bench;
player.place = place;
lineup.players.push(player);
/*
MISSING: Add field or bench player and rearrange squad
*/
// If any of rule restrictions evaluates to true ...
// Using lodash _.some with out second argument which defaults to _.identity
if ( _.some([ lineup.reachesMaxForeignPlayers(),
lineup.reachesBudgetLimit(),
lineup.reachesMaxSameTeamLimit(),
lineup.reachesMaxSameFavoriteTeamLimit()]) ) {
response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}
// Whole rule validations passed through
else {
lineup.save().then(function (saved) {
// Pluck missing, valid while DEV
return response.send(202,JSON.stringify(saved));
});
}
}).
catch(function (err) {
console.log(err);
response.send(500,JSON.stringify(err));
})
}
方法二
deletePlayer: function (request,response) {
console.log("deletePlayer");
var lineupId = request.param('id');
var playerId = request.param('player');
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
_.remove(foundLineUp.players, function(player) {
console.log(player.id + " || " + playerId);
return player.id === playerId;
});
console.log("Ended remove");
foundLineUp.save().then(function (savedLineup) {
console.log("Saved lineup\n\n:" + JSON.stringify(savedLineup));
return response.send(202, JSON.stringify(savedLineup));
}).catch(function (err) {
console.log("save lineup err");
response.send(500, JSON.stringify(err));
});
}).catch(function (err) {
console.log(err);
return response.send(500, JSON.stringify(err));
});
}
我认为这是一个超时,因为你没有返回你在承诺中解决的问题:
addPlayer: function(request, response) {
console.log("Add player");
var lineupId = request.params.id;
var receivedPlayer = request.param('player');
var playerId = receivedPlayer.id;
var bench = receivedPlayer.bench;
var place = receivedPlayer.place;
return LineUp.findOne().where({
id: lineupId
}).then(function(foundLineUp) {
return Player.findOne().where({
id: playerId
}).then(function(player) {
console.log("Found player with _id " + player.id);
return [foundLineUp, player];
});
}).spread(function(lineup, player) {
if (!lineup)
return response.send(404);
if (!player)
return response.send(409, "El jugador " + playerId + " no existe");
else if (!lineup.formation)
return response.send(409, "No se ha elegido una táctica para esta alineación");
if (lineup.squadIsComplete())
return response.send(409, "La plantilla ya contiene el máximo de 15 jugadores");
if (lineup.playerWasAdded(player.id))
return response.send(409, "El jugador ya ha sido agregado a la alineación");
if (lineup.fieldIsComplete() && !bench)
response.send(409, "Ya se han agregado los 11 jugadores de campo");
/*If there's already a player with same position, return 409, should modify*/
player.bench = bench;
player.place = place;
lineup.players.push(player);
/*
MISSING: Add field or bench player and rearrange squad
*/
// If any of rule restrictions evaluates to true ...
// Using lodash _.some with out second argument which defaults to _.identity
if (_.some([lineup.reachesMaxForeignPlayers(),
lineup.reachesBudgetLimit(),
lineup.reachesMaxSameTeamLimit(),
lineup.reachesMaxSameFavoriteTeamLimit()
])) {
response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}
// Whole rule validations passed through
else {
lineup.save().then(function(saved) {
// Pluck missing, valid while DEV
return response.send(202, JSON.stringify(saved));
});
}
}).
catch(function(err) {
console.log(err);
response.send(500, JSON.stringify(err));
})
}
在方法 1 中你有:
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
var player = Player.findOne().where({id: playerId}).then(function (player) {
console.log("Found player with _id " + player.id);
return player;
});
return [foundLineUp,player];
}).spread(function (lineup,player){...
当您调用 'then' 时:
var player = Player.findOne().where({id: playerId}).then(...)
您正在返回 'player',但它在回调正文中返回。
这并不意味着这将在行之前执行:
return [foundLineUp,player];
即'spread'的输入。
这应该有效:
LineUp.findOne().where({
id: lineupId
}).then(function(foundLineUp) {
return Player.findOne().where({
id: playerId
}).then(function(player) {
console.log("Found player with _id " + player.id);
return [foundLineUp, player];
});
}).spread(function(lineup, player) {....
在这里你正在等待承诺被解决并且球员和阵容值在一个数组中返回,所以传播可以继续。
使用 sails.js 我得到了这些执行数据库请求和使用承诺的控制器方法。问题是它们正在工作,但是当执行请求时,客户端会收到 504。
我想确保我的实现是好的,并且 504 在 sails/node 编码中不是问题,而是任何 nginx 或其他东西。我在 config/logs.js
中将日志模式设置为愚蠢,但每次调用 response.send
时我什至看不到。
下面的两种方法都存在这样的问题,但(也许是巧合)第一种方法只是有时会发生。
方法一
addPlayer: function (request,response) {
console.log("Add player");
var lineupId = request.params.id;
var receivedPlayer = request.param('player');
var playerId = receivedPlayer.id;
var bench = receivedPlayer.bench;
var place = receivedPlayer.place;
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
var player = Player.findOne().where({id: playerId}).then(function (player) {
console.log("Found player with _id " + player.id);
return player;
});
return [foundLineUp,player];
}).spread(function (lineup,player) {
if (!lineup)
return response.send(404);
if (!player)
return response.send(409, "El jugador " + playerId + " no existe");
else if (!lineup.formation)
return response.send(409, "No se ha elegido una táctica para esta alineación");
if (lineup.squadIsComplete())
return response.send(409, "La plantilla ya contiene el máximo de 15 jugadores");
if (lineup.playerWasAdded(player.id))
return response.send(409, "El jugador ya ha sido agregado a la alineación");
if (lineup.fieldIsComplete() && !bench)
response.send(409, "Ya se han agregado los 11 jugadores de campo");
/*If there's already a player with same position, return 409, should modify*/
player.bench = bench;
player.place = place;
lineup.players.push(player);
/*
MISSING: Add field or bench player and rearrange squad
*/
// If any of rule restrictions evaluates to true ...
// Using lodash _.some with out second argument which defaults to _.identity
if ( _.some([ lineup.reachesMaxForeignPlayers(),
lineup.reachesBudgetLimit(),
lineup.reachesMaxSameTeamLimit(),
lineup.reachesMaxSameFavoriteTeamLimit()]) ) {
response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}
// Whole rule validations passed through
else {
lineup.save().then(function (saved) {
// Pluck missing, valid while DEV
return response.send(202,JSON.stringify(saved));
});
}
}).
catch(function (err) {
console.log(err);
response.send(500,JSON.stringify(err));
})
}
方法二
deletePlayer: function (request,response) {
console.log("deletePlayer");
var lineupId = request.param('id');
var playerId = request.param('player');
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
_.remove(foundLineUp.players, function(player) {
console.log(player.id + " || " + playerId);
return player.id === playerId;
});
console.log("Ended remove");
foundLineUp.save().then(function (savedLineup) {
console.log("Saved lineup\n\n:" + JSON.stringify(savedLineup));
return response.send(202, JSON.stringify(savedLineup));
}).catch(function (err) {
console.log("save lineup err");
response.send(500, JSON.stringify(err));
});
}).catch(function (err) {
console.log(err);
return response.send(500, JSON.stringify(err));
});
}
我认为这是一个超时,因为你没有返回你在承诺中解决的问题:
addPlayer: function(request, response) {
console.log("Add player");
var lineupId = request.params.id;
var receivedPlayer = request.param('player');
var playerId = receivedPlayer.id;
var bench = receivedPlayer.bench;
var place = receivedPlayer.place;
return LineUp.findOne().where({
id: lineupId
}).then(function(foundLineUp) {
return Player.findOne().where({
id: playerId
}).then(function(player) {
console.log("Found player with _id " + player.id);
return [foundLineUp, player];
});
}).spread(function(lineup, player) {
if (!lineup)
return response.send(404);
if (!player)
return response.send(409, "El jugador " + playerId + " no existe");
else if (!lineup.formation)
return response.send(409, "No se ha elegido una táctica para esta alineación");
if (lineup.squadIsComplete())
return response.send(409, "La plantilla ya contiene el máximo de 15 jugadores");
if (lineup.playerWasAdded(player.id))
return response.send(409, "El jugador ya ha sido agregado a la alineación");
if (lineup.fieldIsComplete() && !bench)
response.send(409, "Ya se han agregado los 11 jugadores de campo");
/*If there's already a player with same position, return 409, should modify*/
player.bench = bench;
player.place = place;
lineup.players.push(player);
/*
MISSING: Add field or bench player and rearrange squad
*/
// If any of rule restrictions evaluates to true ...
// Using lodash _.some with out second argument which defaults to _.identity
if (_.some([lineup.reachesMaxForeignPlayers(),
lineup.reachesBudgetLimit(),
lineup.reachesMaxSameTeamLimit(),
lineup.reachesMaxSameFavoriteTeamLimit()
])) {
response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}
// Whole rule validations passed through
else {
lineup.save().then(function(saved) {
// Pluck missing, valid while DEV
return response.send(202, JSON.stringify(saved));
});
}
}).
catch(function(err) {
console.log(err);
response.send(500, JSON.stringify(err));
})
}
在方法 1 中你有:
LineUp.findOne().where({id: lineupId}).then(function (foundLineUp) {
var player = Player.findOne().where({id: playerId}).then(function (player) {
console.log("Found player with _id " + player.id);
return player;
});
return [foundLineUp,player];
}).spread(function (lineup,player){...
当您调用 'then' 时:
var player = Player.findOne().where({id: playerId}).then(...)
您正在返回 'player',但它在回调正文中返回。
这并不意味着这将在行之前执行:
return [foundLineUp,player];
即'spread'的输入。
这应该有效:
LineUp.findOne().where({
id: lineupId
}).then(function(foundLineUp) {
return Player.findOne().where({
id: playerId
}).then(function(player) {
console.log("Found player with _id " + player.id);
return [foundLineUp, player];
});
}).spread(function(lineup, player) {....
在这里你正在等待承诺被解决并且球员和阵容值在一个数组中返回,所以传播可以继续。