文档未删除 findoneanddelete mongoose

时间:2021-07-31 19:38:11

标签: javascript node.js mongoose

我有这个应该删除文档的代码:

const groups = require('./groups')
const ingroups = require('./ingroups')
    const leavegroup = await ingroups.findOne({User:message.author.id})
    //Remove 1 from the member count
    if (!leavegroup) {
    return message.reply('You are not in a group and therefore you cannot leave one.')
}
    message.reply('You have now left the group and are free to go join another one.')
    ingroups.findOneandDelete({User:message.author.id})
  }

它返回您现在已经离开了群组,可以自由地加入另一个群组,但实际上并没有删除数据库中的文档。

如何让它删除文档?

1 个答案:

答案 0 :(得分:1)

您的逻辑结构不正确,而且 findOneAndDelete 有大小写错误。请使用以下建议使其工作。

const groups = require('./groups')
const ingroups = require('./ingroups')
const leavegroup = await ingroups.findOne({ User: message.author.id })
if (!leavegroup) {
    return message.reply('You are not in a group and therefore you cannot leave one.')
} else {
    await ingroups.findOneAndDelete({ User: message.author.id })
    return message.reply('You have now left the group and are free to go join another one.')
}
相关问题