When the following route is called in express, it is actually executed 6 times. The console.log is printed 6 times, and my mongoose logic is executed 6 times (saves 6 time in database).
I then get returned a http 500 from cloud9ide “Could not proxy request”. I am really confused, I have no loops in my code, how can this happen? The console.log(“in else (2)”); get printed 6 times.
Edit: I have tried the mongooseLogic code with various parts commented out, and the issue was still there. This looks like it isn’t a mongoose issue.
Second edit: I have changed the post for get and hardcoded the body that would be sent, and the route was executed only once.
Third edit: I am also using everyauth for session/authentication with the facebook oauth.
app.post("/result/:userId/:elementId", function(req, res) {
var receivedJSON = req.body;
console.log("In route");
//Making sure the receive request is valid
if(typeof(receivedJSON.type) !== undefined) {
mongooseLogic.saveResults(req.params.elementId, receivedJSON, req.params.userId, function(message) {
if(message === "Success") {
res.json({ success: true, message: 'Result saved.'});
}
else {
res.json({ success: false, message: 'Error in saving results. Trace: ' + message});
}
});
}
else {
res.json({ success: false, message: 'Failed, Invalid object sent to server'});
}
});
Code on the mongooseLogic file:
var saveResults = function(elementRefId, receivedResult, userId, callback){
if(elementRefId.toString().length !== 24 ){
callback("Invalid objectId for elementId");
}
else{
Result.findOne({ "id" : userId, "serieResult.id": elementRefId }, function(err, result){
if(err){
callback("No Document found: " + err);
}
else if( result === null){
console.log("in null");
var tempResult = {
id : elementRefId,
unit : receivedResult.unit,
value : receivedResult.value
}
Result.update({ "id" : userId}, { $push: {"serieResult": tempResult}}, {upsert: true}, function(err){
if(err){
callback("Error in saving result (Distance): " + err);
}
else{
callback("Success");
}
});
}
else{
Result.update({ "id" : userId, "serieResult.id": elementRefId },
{ $set:{
"serieResult.$.unit" : receivedResult.unit,
"serieResult.$.value" : receivedResult.value,
},{upsert: true}, function(err){
if(err){
callback("Cant update doc: " + err);
}
else{
console.log("in else (2)");
callback("Success");
}
});
}
});
}
}
}
This was a problem with the Cloud9 proxy that interfered. The problem was adressed thanks to this issue and has been resolved.