Node.js model.update()未更新
我正在尝试向数组中添加新元素 代码如下:Node.js model.update()未更新,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在尝试向数组中添加新元素 代码如下: var findEditThenSave = function(personId, done) { var foodToAdd = 'hamburger'; var foodArray = []; Person.findById(personId, function (err, data) { if (err) return console.log(err); done(null, data); foodArra
var findEditThenSave = function(personId, done) {
var foodToAdd = 'hamburger';
var foodArray = [];
Person.findById(personId, function (err, data) {
if (err) return console.log(err);
done(null, data);
foodArray = data.favoriteFoods;
console.log("foodArray inside findById: ", foodArray);
foodArray.push(foodToAdd);
var updateObj = {favoriteFoods: foodArray};
console.log(updateObj)
Person.update({_id: personId}, updateObj, function(err, raw) {
if (err) {
console.log("There was an error");
}
console.log("Updated successfully");
console.log("foodArray inside update function:", foodArray);
});
});
};
这是关于故障的全部代码:
这是POST请求的控制台日志:
POST
foodArray inside findById: ["spaghetti"]
{ favoriteFoods: ["spaghetti","hamburger"] }
(node:8943) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
Updated successfully
foodArray inside update function: ["spaghetti","hamburger"]
在进行这些更新时,可以使用async和Wait
var findEditThenSave = async function(personId, done){
var foodToAdd = 'hamburger';
var foodArray = [];
var updateObj;
try{
var data=await Person.findById(personId);
done(null, data);
foodArray = data.favoriteFoods;
console.log("foodArray inside findById: ", foodArray);
foodArray.push(foodToAdd);
updateObj = {favoriteFoods: foodArray};
console.log(updateObj)
}catch(err){
console.log(err);
}
try{
await Person.update({_id: personId}, updateObj);
console.log("Updated successfully");
console.log("foodArray inside update function:", foodArray);
}catch(err){
console.log(err);
}
};
在进行这些更新时,可以使用async和Wait
var findEditThenSave = async function(personId, done){
var foodToAdd = 'hamburger';
var foodArray = [];
var updateObj;
try{
var data=await Person.findById(personId);
done(null, data);
foodArray = data.favoriteFoods;
console.log("foodArray inside findById: ", foodArray);
foodArray.push(foodToAdd);
updateObj = {favoriteFoods: foodArray};
console.log(updateObj)
}catch(err){
console.log(err);
}
try{
await Person.update({_id: personId}, updateObj);
console.log("Updated successfully");
console.log("foodArray inside update function:", foodArray);
}catch(err){
console.log(err);
}
};
正如您在控制台中看到的:
(node:8943) DeprecationWarning: collection.update is
deprecated. Use updateOne, updateMany, or bulkWrite instead.
Updated successfully
因此,您可以使用Person.updateOne而不是Person.updateOne来完成此任务
希望它对您有所帮助,您可以在控制台中看到:
(node:8943) DeprecationWarning: collection.update is
deprecated. Use updateOne, updateMany, or bulkWrite instead.
Updated successfully
因此,您可以使用Person.updateOne而不是Person.updateOne来完成此任务
希望它对您有所帮助如果您只是计划更新文档并返回更新后的文档,您可以这样做 函数updatepersonId{ var foodToAdd=汉堡; const person=person.findByIdAndUpdate 人格, {$push:{favoriteFoods:FoodLoadd}, {新:正确}, 函数错误、结果错误{ if err console.logerr; else console.logresult; } ; };
如果您只是计划更新一个文档并返回更新后的文档,您可以执行以下操作 函数updatepersonId{ var foodToAdd=汉堡; const person=person.findByIdAndUpdate 人格, {$push:{favoriteFoods:FoodLoadd}, {新:正确}, 函数错误、结果错误{ if err console.logerr; else console.logresult; } ; };
仍然没有更新。请立即检查!updateObj未定义,因为它的作用域在第一个try-catch块中。现在我已经使它对两个try-catch块都可见。现在mongoDB数据库似乎是空的,但我在findById:[意大利面条]中得到了日志foodArray,所以我猜它不可能是空的。mongoDB Atlas的延迟是否会导致这种情况?Atlas不显示当前状态是正常的吗?在Atlas中更新确实需要一些时间。请稍后登记/如果我的答案对你有帮助,请投票@Imestin!仍然没有更新。请立即检查!updateObj未定义,因为它的作用域在第一个try-catch块中。现在我已经使它对两个try-catch块都可见。现在mongoDB数据库似乎是空的,但我在findById:[意大利面条]中得到了日志foodArray,所以我猜它不可能是空的。mongoDB Atlas的延迟是否会导致这种情况?Atlas不显示当前状态是正常的吗?在Atlas中更新确实需要一些时间。请稍后登记/如果我的答案对你有帮助,请投票@Imestin!是的,这肯定会解决问题,但我需要用model.update来做这件事,因为这是一个免费的codecamp课程。其他方法都奏效了。在本课中,它希望教授遗留方法。如果它完全不工作了,我应该写信给fCC说它不工作了,但我认为它可以以某种方式工作。是的,这肯定会解决问题,但我需要用model.update来做这件事,因为这是免费的codecamp课程。其他方法都奏效了。在本课中,它希望教授遗留方法。如果它完全不起作用了,我应该写信给联邦通信委员会说它不起作用了,但我认为它可以起作用。