我是Node.js的新手,我遇到了一个错误:
RangeError:已超过最大调用堆栈大小
我无法解决这个问题,因为其他堆栈问题中的大多数堆栈溢出问题都涉及数百个Node.js回调,但我这里只有3个。
首先是fetch (findById
),然后是update,然后是save操作!
我的代码是:
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,所以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
});
});
但这也无济于事,错误还在继续,我不知道如何调试它:/
发布于 2013-02-22 10:05:02
我也一直认为这是个问题。基本上,当你有一个带有ref
的属性,例如,你想在一个查找中使用它,你不能传递整个文档。
例如:
Model.find().where( "property", OtherModelInstance );
这将触发该错误。
但是,现在有两种方法可以解决这个问题:
Model.find().where( "property", OtherModelInstance._id );
// or
Model.find().where( "property", OtherModelInstance.toObject() );
这可能会暂时停止你的问题。
在他们的GitHub repo中有一个问题,我在那里报告了这个问题,但是现在还没有修复。See the issue here。
发布于 2014-06-30 10:07:53
我不断得到这个错误,并最终找出了它。它很难调试,因为在错误中没有显示真实的信息。
原来我是想把一个对象保存到一个字段中。只保存对象的特定属性,或者JSON将其串行化,效果非常好。
看起来如果驱动程序给出一个更具体的错误会更好,但是哦,好吧。
发布于 2015-10-09 00:12:30
MyModel.collection.insert
原因:
RangeError:已超过最大调用堆栈大小
当您传递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);
https://stackoverflow.com/questions/10678156
复制相似问题