Node.js は初めてで、エラーに直面しています:
RangeError: 最大呼び出しスタック サイズを超えました
Node.jsに関する他のstackoverflowの質問のスタックの問題のほとんどは何百ものコールバックを扱っているため、問題を解決できませんが、ここには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を使用しているので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
});
});
しかし、エラーがまだ発生していて、デバッグ方法がわかりません:/