Mandar Kawtakwar
Mandar Kawtakwar

Reputation: 107

express delete route not found

I am trying this code to delete specific record in mongo by id

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 :

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();
    });
}

Upvotes: 1

Views: 2059

Answers (2)

Medet Tleukabiluly
Medet Tleukabiluly

Reputation: 11930

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('/contactlist/'+ id).success( function(response) {
    refresh();
});

Make id param in your server.

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);
    });
});

Upvotes: 2

Nalla Srinivas
Nalla Srinivas

Reputation: 933

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

Upvotes: 0

Related Questions