【发布时间】:2022-01-14 12:19:19
【问题描述】:
大家好,我在尝试删除数据库中的条目时收到此消息。 我在 req.body 上尝试过 JSON.parse,在我的路由文件中切换了路由,但似乎没有任何效果。
这是我的控制器:
async function removeToy(req, res) {
try {
const { toyId } = req.params
const removedId = await toyService.remove(toyId)
res.send(removedId)
} catch (err) {
logger.error('Failed to remove toy', err)
res.status(500).send({ err: 'Failed to remove toy' })
}
}
这是服务:
async function remove(toyId) {
try {
const collection = await dbService.getCollection('toy')
await collection.deleteOne({ '_id': ObjectId(toyId)})
return toyId
} catch (err) {
logger.error(`cannot remove toy ${toyId}`, err)
throw err
}
}
感谢任何形式的帮助!
【问题讨论】:
标签: javascript node.js mongodb express