首页
学习
活动
专区
工具
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

回答 4

Stack Overflow用户

发布于 2013-02-22 10:05:02

我也一直认为这是个问题。基本上,当你有一个带有ref的属性,例如,你想在一个查找中使用它,你不能传递整个文档。

例如:

代码语言:javascript
复制
Model.find().where( "property", OtherModelInstance );

这将触发该错误。

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

代码语言:javascript
复制
Model.find().where( "property", OtherModelInstance._id );
// or
Model.find().where( "property", OtherModelInstance.toObject() );

这可能会暂时停止你的问题。

在他们的GitHub repo中有一个问题,我在那里报告了这个问题,但是现在还没有修复。See the issue here

票数 22
EN

Stack Overflow用户

发布于 2014-06-30 10:07:53

我不断得到这个错误,并最终找出了它。它很难调试,因为在错误中没有显示真实的信息。

原来我是想把一个对象保存到一个字段中。只保存对象的特定属性,或者JSON将其串行化,效果非常好。

看起来如果驱动程序给出一个更具体的错误会更好,但是哦,好吧。

票数 5
EN

Stack Overflow用户

发布于 2015-10-09 00:12:30

MyModel.collection.insert原因:

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

当您传递MyModel实例的数组而不是仅包含该对象的值的数组时。

RangeError:

代码语言:javascript
复制
let myArray = [];

myArray.push( new MyModel({ prop1: true, prop2: false }) );

MyModel.collection.insert(myArray, callback);

无错误:

代码语言:javascript
复制
let myArray = [];

myArray.push( { prop1: true, prop2: false } );

MyModel.collection.insert(myArray, callback);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10678156

复制
相关文章

相似问题

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