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

prd*_*dox 18 mongoose mongodb node.js express

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

RangeError:超出最大调用堆栈大小

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

首先是一个fetch(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
                });

            });
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

最后在保存操作期间发生错误!

我使用MongoDB的与猫鼬所以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
    });

});
Run Code Online (Sandbox Code Playgroud)

但它仍然没有帮助错误,我不知道如何调试它:/

gus*_*nke 21

我也一直在试图解决这个问题.基本上,当你有一个带有a的属性ref,并且你想在一个find中使用它时,你就无法传递整个文档.

例如:

Model.find().where( "property", OtherModelInstance );
Run Code Online (Sandbox Code Playgroud)

这将触发该错误.

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

Model.find().where( "property", OtherModelInstance._id );
// or
Model.find().where( "property", OtherModelInstance.toObject() );
Run Code Online (Sandbox Code Playgroud)

这可能暂时阻止你的问题.

他们的GitHub回购中有一个问题,我报告了这个问题,但现在还没有修复.在这里查看问题.


rgo*_*ger 5

我一直收到这个错误,最后弄明白了。由于错误中没有真实的信息,因此调试起来非常困难。

原来,我试图将对象保存到字段中。仅保存对象的特定属性或JSON对其进行字符串化处理,就像一种魅力。

如果驱动程序给出了更具体的错误,这似乎很好,但是,哦。


Jar*_*yth 3

有几种方法可以调试nodejs应用程序

内置调试器

Node.js 调试器记录如下: http: //nodejs.org/api/debugger.html

要放置断点,只需将其放置debugger;在要断点的位置即可。正如你所说table.save回调给您带来了麻烦,您可以在该函数内放置一个断点。

然后在启用调试器的情况下运行节点:

node debug myscript.js
Run Code Online (Sandbox Code Playgroud)

并且您将获得更多有用的输出。

调查堆栈

console.trace如果您很清楚何时/何处遇到问题,并且想要弄清楚如何到达那里,您还可以使用打印堆栈跟踪。

祝你好运,我希望这有帮助!