gpt4 book ai didi

node.js - 错误:Can't canonicalize query: BadValue Unsupported projection option

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:48 24 4
gpt4 key购买 nike

用户架构

var UserSchema = new Schema({

name: String,
username: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true, select: false},
favouriteid:[{eventid:String}]

});

事件架构

var EventSchema=new Schema({
name:String,
location:{ type:String },
description:{type:String },
price: String,
rating: {
value: String,
count: {type: String, default: 10},
userrating: [{
uservalue: String,
userid: String
}]
},
imageurl:[String],
userimageurl:[String],


reviews:[{ userid:String,
username: String,
comment:String}]

});

POST 方法在事件架构中推送 userid 和 uservalue 的值。

api.post('/rateevent', function (req, res) {

var userid = req.body.userid;
var uservalue = req.body.uservalue;
var eventid = req.body.eventid;

Event.findById({_id: eventid},
{$push: {rating: {userrating: {uservalue: uservalue, userid: userid}}}},
{upsert: true},
function (err, events) {
if (err) {
res.send(err);
return;
}
else {
calculaterating(events);
}
});

function calculaterating(event) {

event.rating.count++;
event.rating.value = (event.rating.value * (event.rating.count - 1) + uservalue) / event.rating.count;


res.json("rating updated");


}


});

显示以下错误:

{
"name": "MongoError",
"message": "Can't canonicalize query: BadValue Unsupported projection option: $push: { rating: { userrating: { uservalue: \"5\", userid: \"56593f3657e27af8245735d7\" } } }",
"$err": "Can't canonicalize query: BadValue Unsupported projection option: $push: { rating: { userrating: { uservalue: \"5\", userid: \"56593f3657e27af8245735d7\" } } }",
"code": 17287
}

是不是post方法不对?我看过其他 mongodb 文档但找不到这种类型的东西。我是 Node js新手。帮助我。

最佳答案

它应该是Event.update而不是Event.findById,而且你的推送操作看起来是错误的。应该是这样的:

Event.findOneAndUpdate(
{_id: eventid},
{$push: {'rating.userrating': {uservalue: uservalue, userid: userid}}},
{new: true},
function (err, events) {
if (err) {
res.send(err);
return;
}
else {
if(events.length > 0){
calculaterating(events);
}
else {
res.json({msg: "Nothing to update"});
}
}
});

function calculaterating(event) {
event = event[0]; //get the object from array
event.rating.count++;
event.rating.value = (event.rating.value * (event.rating.count - 1) + uservalue) / event.rating.count;
Event.update(
{_id: eventid},
{$set: {
'rating.count': event.rating.count,
'rating.value': event.rating.value
}},
function(err, response){
if (err) {
res.send(err);
return;
}
else {
res.json({msg: "rating updated"});
}
});
}

在事件变量中,您将获得在新状态下更新的文档。如果您已通过 {new: false},您将获得更新前的文档。

关于node.js - 错误:Can't canonicalize query: BadValue Unsupported projection option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33971131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com