小编Hon*_*bun的帖子

MongoDB .deleteOne 无法在 Node.js 中工作

因此,我试图在按下按钮时删除文档。按钮代码如下所示:

<form action="/patients/delete?_method=DELETE" method="POST">
    <input type="hidden" id="patientID" name="patientID" value=' _id: <%= patient._id%>'>
    <button type="submit" class="btn btn-primary">Delete this patient from the Database</button>
</form>
Run Code Online (Sandbox Code Playgroud)

我设置了一条如下所示的路线:

router.delete('/delete', AuthControl.checkLoggedIn, patientsControl.patientDelete);
Run Code Online (Sandbox Code Playgroud)

它在患者控件中调用此函数:

    patientDelete = async (req, res) => {
    let DeleteParam = req.body.patientID;
    console.log(DeleteParam);
    let DeleteConfirm = await this.patientsModel.DeletePatient(DeleteParam);
    if (DeleteConfirm) {
        res.render('patients');
    } else {
        console.log("Error happening somewhere");
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在患者模型中调用此函数:

async DeletePatient(DeleteParam) {
    return mongoose.connection.db.collection("PatientsDB").
    deleteOne({ _id : DeleteParam);
}
Run Code Online (Sandbox Code Playgroud)

//编辑:快速修复了上面的代码,这不是我正在运行的返回 true 的代码,因为我没有在上面的 PatientDelete 中记录错误。

console.log(DeleteParam); 正在返回我要删除的文档的 id,如下所示: 5f22dc43b1e72e9769263810

我试图删除的文档如下所示:

_id : 5f22dc43b1e72e9769263810 …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongodb-query

5
推荐指数
1
解决办法
5369
查看次数

标签 统计

mongodb ×1

mongodb-query ×1

mongoose ×1

node.js ×1