简体   繁体   English

找不到快速删除路线

[英]express delete route not found

I am trying this code to delete specific record in mongo by id 我正在尝试此代码通过ID删除mongo中的特定记录

where in angular I'm passing the Id from the controller It gives error code saying 404 not found code in Node server side file is : 我从控制器传递Id的角度是什么,它给出错误代码,表明在节点服务器端文件中找不到404的代码是:

app.delete('/contactlist/id', function (req, res) {
var id = req.params.id;
console.log("in delete"+ id);
db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
    res.json(doc);
  });
});

and code in controller of angular : 和角度控制器中的代码:

    $scope.remove = function (id){
    console.log(id);
    $http.delete('/contactlist'+ id ).success( function(response) {
        refresh();
    });
}

Everything looks ok, except 一切看起来不错,除了

$http.delete('/contactlist'+ id ) //you forgot additional slash, '/contactlist/'+ id
.success( function(response) {
    refresh();
});

And

app.delete('/contactlist/id', function (req, res) { //this must be a param '/contactlist/:id'
    var id = req.params.id;
    console.log("in delete"+ id);
    db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
        res.json(doc);
    });
});

In summary 综上所述

Add slash to $http.delete $http.delete添加斜杠

$http.delete('/contactlist/'+ id).success( function(response) {
    refresh();
});

Make id param in your server. 在服务器中设置id参数。

app.delete('/contactlist/:id', function (req, res) { //colons are important
    var id = req.param.id;
    console.log("in delete"+ id);
    db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
        res.json(doc);
    });
});

Try below code 试试下面的代码

 app.delete('/contactlist/:id', function (req, res) {
 var id = req.params.id;
console.log("in delete"+ id);
db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
res.json(doc);
});
});

Your can see the doc from here to pass params with the url: http://expressjs.com/en/api.html 您可以从此处查看该文档以使用url传递参数: http : //expressjs.com/en/api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM