Node.js 错误{MongoError:未知修饰符:$pushAll,当使用mongoose更新时
我正在将AngularJS应用程序转换为Angular 7,它们共享一个后端API。我正在提交相同的数据,并且它的日志记录正确,但在尝试保存记录时,我在后端返回了一个关于未知修饰符$pushAll的错误 我的控制器代码:Node.js 错误{MongoError:未知修饰符:$pushAll,当使用mongoose更新时,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在将AngularJS应用程序转换为Angular 7,它们共享一个后端API。我正在提交相同的数据,并且它的日志记录正确,但在尝试保存记录时,我在后端返回了一个关于未知修饰符$pushAll的错误 我的控制器代码: Volunteer .findById(volunteerId) .select('shifts') .exec(function(err, volunteer) { var thisShift; var response
Volunteer
.findById(volunteerId)
.select('shifts')
.exec(function(err, volunteer) {
var thisShift;
var response = {
status : 200,
message : {}
};
if (err) {
console.log("Error finding volunteer request");
response.status = 500;
response.message = err;
} else if(!volunteer) {
console.log("Volunteer request not found", volunteerId);
response.status = 404;
response.message = {
"message" : "Volunteer request not found " + volunteerId
};
} else {
// Get the shift
thisShift = volunteer.shifts.id(shiftId);
console.log("SHIFT BEFORE UPDATE", JSON.stringify(thisShift));
// If the shift doesn't exist Mongoose returns null
if (!thisShift) {
response.status = 404;
response.message = {
"message" : "Shift not found " + shiftId
};
}
}
if (response.status !== 200) {
res
.status(response.status)
.json(response.message);
} else {
thisShift.volleyteers.push({
fullname: user.name,
phone: user.phone,
email: user.username
});
console.log("thisShift to be saved", JSON.stringify(thisShift));
volunteer.save(function(err, volunteerUpdated) {
if (err) {
console.log("ERROR",err);
res
.status(500)
.json(err);
} else {
console.log("SUCCESS!");
res
.status(204)
.json({message: "Thank you for volunteering! You can see where you need to on your profile page or someone will contact you."});
}
});
}
});
};
我得到的错误以及后端的console.logged文件是:
我正在使用:
- 节点11.13.0
- MongoDB服务器版本:4.0.8
- “猫鼬”:“^4.9.6”
$pullAll
在MongoDB中不受欢迎,但mongoose在幕后使用它
解决方法似乎是在5.0.0-rc2版本之前,根据添加usePushEach:true
这段代码似乎是原因:
thisShift.volleyteers.push({
fullname: user.name,
phone: user.phone,
email: user.username
});
因为您要求mongoose保存一个对象数组
猫鼬不在幕后使用的另一个解决办法似乎是尝试使用concat:
thisShift.volleyteers.concat([{
fullname: user.name,
phone: user.phone,
email: user.username
}]);
正如一样,我也遇到了同样的问题。问题是$pushall函数已被弃用,但一些旧版本的Mongoose仍在使用它。我将Mongoose升级到5.6.11版(最新版本),这个问题就消失了 p/S:如果您使用mongoose unique validator插件,您也应该升级它(我升级到2.0.3版) 希望能有帮助