Node.js + mongoose

我是Node新手,我正面临一个错误:

[RangeError:超出最大调用堆栈大小]

我无法解决这个问题,因为大多数其他堆栈问题stackoverflow有关节点处理数百callback的问题,但我只有3在这里。

首先获取(findbyid)然后更新一个保存操作!

我的代码是:

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}); }); }); })}); 

在保存操作结束时发生错误!

我用mongoose使用MongoDb所以'表'和'用户'是我的数据库集合。

这是从我的第一个项目与nodejs,expressjs和mongodb,所以我可能已经在asynchronous代码:(

编辑 :我试图用更新replace保存:

 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}); }); 

但它不能帮助错误仍然来了,我不知道如何debugging它:/

我也一直在传递这个问题。 基本上,当你有一个财产与ref ,而你想用它在find,例如,你不能传递整个文件。

例如:

 Model.find().where( "property", OtherModelInstance ); 

这会触发这个错误。

但是,现在有两种方法可以解决这个问题:

 Model.find().where( "property", OtherModelInstance._id ); // or Model.find().where( "property", OtherModelInstance.toObject() ); 

这可能暂时停止你的问题。

在他们的GitHub仓库中有一个问题,我报告了这个问题,但现在还没有解决。 在这里看到问题 。

有几种方法可以debuggingnodejs应用程序

内置debugging器

Node.jsdebugging器logging在这里: http : //nodejs.org/api/debugger.html

放置断点,简单地把debugger; 在你想要rest的地方 正如你所说的, table.savecallback给你带来麻烦,你可以在这个函数中放置一个断点。

然后在启用debugging器的情况下运行节点:

 node debug myscript.js 

你会得到更多有用的输出。

调查堆栈

你也可以使用console.trace来打印一个堆栈跟踪,如果你有一个好主意你遇到问题的时间/地点,并想知道你是如何到达那里的。

祝你好运,我希望这有助于!

我不断得到这个错误,并最终搞清楚了。 由于没有真正的信息出现在错误中,因此debugging非常困难。

原来我试图把一个对象保存到一个字段中。 只保存对象的特定属性,或JSONstring化,就像一个魅力。

看起来如果司机给出了一个更具体的错误,这将是很好的,但哦。

MyModel.collection.insert原因:

[RangeError:超出最大调用堆栈大小]

当您传递MyModel的实例的MyModel而不是仅使用该对象的值的数组时。

引发RangeError:

 let myArray = []; myArray.push( new MyModel({ prop1: true, prop2: false }) ); MyModel.collection.insert(myArray, callback); 

没有错误:

 let myArray = []; myArray.push( { prop1: true, prop2: false } ); MyModel.collection.insert(myArray, callback);