node.js - How to properly debug the response lifecyle in Sails.js? -
using sails.js got these controller methods perform db requests , use promises. problem they're working when request performed 504 obtained @ client side.
i want make sure implementation , 504 not problem in sails/node coding nginx or else. set log mode silly in config/logs.js
can't see everytime response.send
invoked.
both methods bellow present such issue (maybe coincidence) first method happening sometimes.
method 1
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 _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 la alineación"); if (lineup.fieldiscomplete() && !bench) response.send(409, "ya se han agregado los 11 jugadores de campo"); /*if there's player same position, return 409, should modify*/ player.bench = bench; player.place = place; lineup.players.push(player); /* missing: add field or bench player , rearrange squad */ // if of rule restrictions evaluates true ... // using lodash _.some out second argument defaults _.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)); }) }
method 2
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)); }); }
i think timeout because you're not returning resolved in promise:
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 _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 la alineación"); if (lineup.fieldiscomplete() && !bench) response.send(409, "ya se han agregado los 11 jugadores de campo"); /*if there's player same position, return 409, should modify*/ player.bench = bench; player.place = place; lineup.players.push(player); /* missing: add field or bench player , rearrange squad */ // if of rule restrictions evaluates true ... // using lodash _.some out second argument defaults _.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)); }) }
Comments
Post a Comment