Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Node.js mongoose [RangeError:超过最大调用堆栈大小]大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Node的新手,我面临一个错误

我无解决问题,因为大多数堆栈问题在其他的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所以’表’和’用户’是我的数据库集合。

这是从我的第一个项目,nodejs,expressjs和mongodb,所以我可能在异步代码中犯了很大的错误:(

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

@H_131_14@models.Table.update({_id: table._iD},{ '$push': { players : {userId: req.user._id,cards:{}} } },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}); });

但是它不会帮助错误仍然来临,我不知道如何调试它:

解决方法

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

例如:

@H_131_14@model.find().where( "property",OtherModelInstance );

这将触发该错误

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

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

这可能会阻止你现在的问题。

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

大佬总结

以上是大佬教程为你收集整理的Node.js mongoose [RangeError:超过最大调用堆栈大小]全部内容,希望文章能够帮你解决Node.js mongoose [RangeError:超过最大调用堆栈大小]所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。