我有一个以下代码来更新子集合字段中的文档,我得到的是异常,然后是代码。
router.route('/user/:_id/host/:_id/accept/').post(function(req, res) {
Host.findAndModify({
"query" : {
"_id" : req.body.eventid,
"joinees" : {
"$elemMatch" : {
"userid" : req.body.userid
}
}
},
"update" : {
"$set" : {
"joinees.$.status_code" : 1
},
},
"new" : true,
"upsert" : false
}, function(err, host) {
if (err) {
console.log(err);
res.json(err);
} else {
console.log("kanishka");
res.json(host);
}
});
});
控制台中打印的异常如下:
TypeError: undefined is not a function
at Object.handle (/node/Playo1/server1.js:332:7)
at next_layer (/node/Playo1/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/node/Playo1/node_modules/express/lib/router/route.js:107:5)
at /node/Playo1/node_modules/express/lib/router/index.js:195:24
at param (/node/Playo1/node_modules/express/lib/router/index.js:268:14)
at param (/node/Playo1/node_modules/express/lib/router/index.js:280:16)
at param (/node/Playo1/node_modules/express/lib/router/index.js:280:16)
at Function.proto.process_params (/node/Playo1/node_modules/express/lib/router/index.js:296:3)
at next (/node/Playo1/node_modules/express/lib/router/index.js:189:19)
at next (/node/Playo1/node_modules/express/lib/router/index.js:166:38)
答案 0 :(得分:4)
试试这个。这将有效。
router.route('/user/:_id/host/:_id/accept/')
.post(function(req, res) {
Host.findOneAndUpdate({
"_id" : req.body.eventid,
"joinees" : {
"$elemMatch" : {
"userid" : req.body.userid
}
}
}, {
"$set" : {
"joinees.$.status_code" : 1
}
}, {
"new" : true,
"upsert" : false
}, function(err, host) {
if (err) {
console.log(err);
res.json(err);
} else {
res.json({
message : "request Accepted",
requestStatus : 1
});
}
});
});