Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoError:在路径'_id';将修改不可变字段'_id';_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js MongoError:在路径'_id';将修改不可变字段'_id';

Node.js MongoError:在路径'_id';将修改不可变字段'_id';,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,每当我尝试更新值的计数时,它都会显示错误: MongoError: Performing an update on the path '_id' would modify the immutable field '_id' 如果没有update函数,代码可以正常工作,但是在put路由中使用updatefind会产生错误 如果polls对象中的某个选项的键值等于通过编辑路由获得的值,则我希望将该选项的值增加1 var express =require("express"); var a

每当我尝试更新
值的计数时,它都会显示错误:

MongoError: Performing an update on the path '_id' would modify the immutable field '_id'
如果没有update函数,代码可以正常工作,但是在put路由中使用updatefind会产生错误

如果polls对象中的某个选项的键值等于通过编辑路由获得的值,则我希望将该选项的值增加1

var express      =require("express");
var app          =express();
var bodyParser   =require("body-parser");
var mongoose     =require("mongoose");
var methodOverride=require("method-override");
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
app.use(methodOverride("_method"));
mongoose.connect("mongodb://localhost/vtapp");
var pollSchema=new mongoose.Schema({
    name:String,
    options:[{key:String,value:Number}]
});
var Poll=mongoose.model("Poll",pollSchema);

app.get("/",function(req,res){
    Poll.find({},function(err,allpolls){
        if(err){
            console.log(err);
        }
        else
        {
            res.render("landing",{polls:allpolls}); 
        }
    });
});
app.post("/",function(req,res){
    var name=req.body.name;
    var text=req.body.options;
    var arrops=text.split("\n");
    var newpoll={name:name,options:[]};
    arrops.forEach(function(opt){
        newpoll.options.push({key:opt,value:0});
    });
    Poll.create(newpoll,function(err,newp){
        if(err){
            console.log(err);
        }
        else
        {
            res.redirect("/");
        }
    });
});
app.get("/new",function(req,res){
    res.render("new");
});
app.get("/pollshow/:id/edit",function(req,res){
    Poll.findById(req.params.id,function(err,foundpoll){
        if(err){
            console.log(err);
        }
        else
        {
            res.render("show",{poll:foundpoll});
        }
    });
});
app.put("/pollshow/:id",function(req,res){
    Poll.findById(req.params.id,function(err,found){
        if(err){
            console.log(err);
        }
        else
        {
            found.options.forEach(function(option){
                if(option.key==req.body.choice){
                    found.update(option,{$inc:{value:1}},function(err){
                        if(err) {console.log(err);}
                    });
                }
            });
            res.redirect("/pollshow/"+req.params.id+'/edit');
        }
    });
});
app.listen(process.env.PORT,process.env.IP,function(){
    console.log("Application is running");
});

如果我没有修改
id
,错误是如何产生的?

您是否能够检测到错误来自何处?我的代码中遇到了类似的错误。。。