我正在使用node,express,mongo开展项目。我想删除一个主题。
这是我的代码:
router.get('/:id/delete', function(req, res, next){
var topicId = req.params.id;
console.log('Logging topic id: ' + topicId);
Topic.findById(topicId, function(err, topic)
{
if(err)
{
console.log('There was no topic with this ID');
return next(err)
}
else
{
Topic.remove(topicId, function(err){
res.render('/mytopics');
console.log('Topic deleted successfuly');
});
}
});
});
事情是它甚至没有进入第一个控制台日志,我记录了主题ID。所以我想知道我的查询是否构建正确吗?
答案 0 :(得分:1)
在RESTful API结构中,delete
路由应该期望DELETE
请求。请尝试以下代码:
router.delete('/:id/delete', function(req, res, next) {
// ...
有关此问题的其他资源,请务必查看:
Recommended way to delete object in MongoDB based on a route