我正在尝试添加一个函数,通过 id 删除 MongoDB 中的记录,但结果得到一个空数组,并且该记录未删除。
这是迄今为止我的代码:
//router
router.delete('/comandas/:id', (req, res) => {
deleteLine(req.params.id)
res.status(500).end()
});
});
//delete function
const objectId = require('mongodb').ObjectID;
const init = () =>
MongoClient.connect(connectionUrl, { useNewUrlParser: true, useUnifiedTopology: true }).then((client) => {
db = client.db(dbName)
})
const deleteLine = (id) => {
const collection = db.collection('comanda')
return collection.deleteOne({"_id": objectId(id)})
}
最佳答案
您在deleteLine函数中返回一个 promise ,在您的路由器中要实际使其运行,您需要像这样添加然后阻止:
deleteLine(req.params.id).then(result => {
console.log(result);
//todo: send response
})
.catch(err => {
console.log(err);
//todo: send error response
})
关于javascript - router.delete 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59681754/