Node.js + Mongoose [RangeError : Maximum call stack size exceeded]

标签 node.js mongodb mongoose express

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

RangeError: Maximum call stack size exceeded

我无法解决问题,因为其他有关 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
                });

            });
        });

    });
});

最后保存操作时出错!

我将 MongoDb 与 mongoose 一起使用,所以 TableUser 是我的数据库集合。

这是我第一个使用 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
    });

});

但这无济于事,错误仍然来了,我不知道如何调试它:/

最佳答案

我也因这个问题而过关。 基本上,当您有一个带有 ref 的属性,并且您想在查找中使用它时,例如,您不能传递整个文档。

例如:

Model.find().where( "property", OtherModelInstance );

这将触发该错误。

但是,您现在有 2 种方法可以解决此问题:

Model.find().where( "property", OtherModelInstance._id );
// or
Model.find().where( "property", OtherModelInstance.toObject() );

这可能会暂时解决您的问题。

我在他们的 GitHub 存储库中报告了这个问题,但目前还没有修复。 See the issue here .

关于Node.js + Mongoose [RangeError : Maximum call stack size exceeded],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10678156/

相关文章:

node.js - $limit 和 $skip 在 MongoDB 聚合管道中作为可选

node.js - mongodb 匹配和组 2 查询合二为一

javascript - MEAN API 请求方法问题

javascript - Mongoose 具有 JSON 数据,其值作为属性名称

javascript - Nest.js - 根据 header 值跳过中间件

node.js - 无服务器框架下载模板时卡住

node.js - 即使 key 不存在,如何插入DynamoDb

mysql - 将日期输入从 HTML 转换为 MySQL DB

mongodb - 将数据从mongodb迁移到hdfs

mongodb - Spring Mongo 仅通过新 POJO 的非空字段进行更新