在数组中使用mongoose.model.update.地图方法,但不工作?



我想在我的数据库中更新一个键的值,但它不工作。请查看下面我的代码。

if (req.body.Body != 'Hi') {
Chat.find({ isBot: true }).sort({ sentTime: -1 }).limit(1).exec(function (err, message) {
message.map(each => {
console.log(each.queryType) 
if (each.queryType === 'ask_email') {  //true
console.log(req.body.Body); //get required data
console.log(each._id); also getting id as plain text
Chat.update({ _id: `ObjectId("${each._id}")` }, { $set: { MessageReceieved: req.body.Body } });
}
else if (message.queryType === 'ask_email' && each.MessageReceieved != null) {
sendMessage('Kindly provide password', from, 'ask_password')
}
else if (each.queryType === 'ask_password' && each.MessageReceieved === null) {
Chat.update({ _id: each._id }, { $set: { MessageReceieved: req.body.Body } });
}
})

});
}

我也尝试过更改为updateone,但没有分辨率。请建议

try this

Chat.findByIdAndUpdate({
_id: each._id
}, {
$set: {
MessageReceieved: req.body.Body
}
}).then(chat => {
// for respond
}).catch(error => {
// for debugin error
})

尝试如下:

Chat.findByIdAndUpdate(each._id, { MessageReceieved: req.body.Body  })

最新更新