我是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
});
});
但这也无济于事,错误还在继续,我不知道如何调试它:/
https://stackoverflow.com/questions/10678156
复制相似问题