我是Node.js的新手,我面临一个错误:
范围错误:超出了最大调用堆栈大小
我无法解决这个问题,因为其他关于Node. js的堆栈溢出问题中的大多数堆栈问题都处理数百个回调,但我这里只有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
});
});
});
});
});
错误发生在最后的保存操作期间!
我将MongoDb与mongoose一起使用,因此Table
和User
是我的数据库集合。
这是我用Node.js、Express.js和MongoDB做的第一个项目,所以我可能在异步代码中犯了巨大的错误:(
编辑:我试图用更新替换保存:
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
});
});
但是无济于事错误还是来了不知道怎么调试:/
这个问题我也一直在传。基本上,当您有一个带有< code>ref的属性,并且您想在find中使用它时,例如,您不能传递整个文档。
例如:
Model.find().where( "property", OtherModelInstance );
这将触发该错误。
但是,您现在有两种方法可以解决此问题:
Model.find().where( "property", OtherModelInstance._id );
// or
Model.find().where( "property", OtherModelInstance.toObject() );
这可能会暂时停止您的问题。
在他们的 GitHub 存储库中有一个问题,我报告了这个问题,但是目前还没有修复。请参阅此处的问题。
我一直收到这个错误,终于想通了。调试非常困难,因为错误中没有显示真实信息。
结果是我试图将一个对象保存到一个字段中。只保存对象的特定属性,或者JSON字符串化它,效果非常好。
看起来如果司机给出一个更具体的错误就好了,但是哦,好吧。
< code > my model . collection . insert 导致:
[RangeError:超出最大调用堆栈大小]
当您传递MyModel
的实例数组而不仅仅是带有该对象值的数组时。
范围错误:
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);