Tuesday, May 22, 2012

Node.js + mongoose [RangeError: Maximum call stack size exceeded]

I am new to Node and im facing an error : [RangeError: Maximum call stack size exceeded]



im not able able to solve the problem because most of the stack problems in others stackoverflow questions about node deals with hundreds of callback but i have only 3 here,



first a fetch (findbyid) then an update an later a save operation !



my code is :



app.post('/poker/tables/:id/join', function(req,res){
var id = req.params.id;

models.Table.findById(id, function (err, table) {
if(err) { console.log(err); res.send( { message: 'error' }); return; }

if(table.players.length >= table.maxPlayers ) {
res.send( { message: "error: Can't join ! the Table is full" });
return;
}
console.log('Table isnt Full');

var BuyIn = table.minBuyIn;
if(req.user.money < table.maxPlayers ) {
res.send( { message: "error: Can't join ! Tou have not enough money" });
return;
}
console.log('User has enought money');

models.User.update({_id:req.user._id},{ $inc:{ money : -BuyIn }}, function(err, numAffected) {
if(err) { console.log(err); res.send( { message: 'error: Cant update your account' }); return; }
console.log('User money updated');


table.players.push( {userId: req.user._id, username: req.user.username, chips:BuyIn, cards:{}} );


table.save(function (err) {
if(err) { console.log(err); res.send( { message: 'error' });return; }

console.log('Table Successfully saved with new player!');
res.send( { message: 'success', table: table});

});
});

})});


The error occurs during the save operation at the end !



I use MongoDb with mongoose so 'Table' and 'User' are my database collections.



This is from my first project with nodejs,expressjs and mongodb so i probably have made huge mistakes in the async code :(



thx for your help !






EDIT: i tried to replace the save with an update :



models.Table.update({_id: table._id}, { '$push': { players : {userId: req.user._id, username: req.user.username, chips:BuyIn, cards:{}} } }, function(err,numAffected) {

if(err) { console.log(err); res.send( { message: 'error' });return; }

console.log('Table Successfully saved with new player!');
res.send( { message: 'success', table: table});


});


But it doesnt help the error is still coming and i dont know how to debug it :/






Last edit: i post the answer here because iam new to this site and it doesnt want me to answer due to the spam protection probably ( or i would have to wait 4 hours and i prefer not !)



I finally got it !



In fact i created another mongoose schema and model for the player witch is an embedded document in the table document but trying to save it directly as an object didnt worked i had to create a specific model instance for the player first and then add it to the table doc so my code is now



... update user money ... then 

var player = new models.Player({userId: req.user._id, username: req.user.username, chips:BuyIn, cards:{}});

models.Table.update({_id: this._id}, { '$push': { 'players' : player } }, function(err,numAffected) {
if(err) { console.log(err); res.send( { message: 'error during update', error: err });return; }

this.players.push( {userId: req.user._id, username: req.user.username, chips:BuyIn, cards:{}} );
res.send( { message: 'success', table: this});

}.bind(this));


I dont really know why mongoose send me a stack size error due to this but it solved the problem !!!





1 comment: