Node.js 在mongoose中删除文档时出现未处理PromisejectionWarning
我正在制作一个webchat网站。这是我的示例代码Node.js 在mongoose中删除文档时出现未处理PromisejectionWarning,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在制作一个webchat网站。这是我的示例代码 const result = await Room.findOne({ name: roomName }); if (result) { result.users.forEach((element, index) => { if (element === userName) { result.users.splice(index, 1); result.sav
const result = await Room.findOne({
name: roomName
});
if (result) {
result.users.forEach((element, index) => {
if (element === userName) {
result.users.splice(index, 1);
result.save();
return;
}
});
if (!result.users.length) {
Room.findOneAndDelete({
name: result.name
}, function(err) {
if (err)
console.log(err);
});
}
}
此代码用于当用户离开房间时,删除房间中的用户名,如果他们的房间没有用户,则删除房间
目前,我的代码删除得很好。。。但是,我总是收到这样的警告
未处理的PromisejectionWarning:VersionError:没有匹配的文档
找到id为“5ecf2ade55c7db23747300e8”的版本0修改路径
“用户”
起初,我以为
findOneAndDelete
函数被调用了两次,但通过控制台检查后,它似乎没有被调用。我使用了Express
和Mongoose
根据Mongoose文档,使用async/wait时必须调用exec()方法
等待MyModel.findOne({}).exec()
检查此链接: