首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js + mongoose [RangeError:超过最大调用堆栈大小]

Node.js + mongoose [RangeError:超过最大调用堆栈大小]
EN

Stack Overflow用户
提问于 2012-05-21 07:45:46
回答 4查看 14.3K关注 0票数 19

我是Node.js的新手,我遇到了一个错误:

RangeError:已超过最大调用堆栈大小

我无法解决这个问题,因为其他堆栈问题中的大多数堆栈溢出问题都涉及数百个Node.js回调,但我这里只有3个。

首先是fetch (findById),然后是update,然后是save操作!

我的代码是:

代码语言:javascript
复制
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,所以TableUser是我的数据库集合。

这是我与Node.js,Express.js和MongoDB的第一个项目,所以我可能在异步代码中犯了很大的错误:(

编辑:我尝试用更新替换保存:

代码语言:javascript
复制
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
    });

});

但这也无济于事,错误还在继续,我不知道如何调试它:/

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10678156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档