1

id i m getting in the controller but its nt coming in the server,when i am clicking on delete i am getting the id in console. its showing error "possibly unhandled rejection" and error 404

this is my server:-

var express=require('express');

var mongojs=require('mongojs');

var bodyParser=require('body-parser');

var app=express();


var db=mongojs('contactlist',['contactlist']);

app.use(express.static(__dirname + "/public"));

app.use(bodyParser.json());

app.get('/Contactlist',function (req,res) {

    console.log("ireceived a get request")


 db.contactlist.find(function (err,docs) {

     console.log(docs);
     res.json(docs);
 })
})
app.post('/Contactlist',function (req,res) {

    db.contactlist.insert(req.body,function (err,doc) {
        res.json(doc);
        console.log(doc);
    })
})
app.delete('/contactlist/:id',function (req,res) {

    var id= req.params.id;
    console.log(id);

})

app.listen(3000);
console.log('Server running on port 3000');

this is my controller

var ContactListApp = angular.module('ContactListApp',[]);
ContactListApp.controller('AppCtrl',[ '$scope','$http',function($scope,$http) {
    console.log("controller");
    var refresh=function () {
        $http.get('/Contactlist').then(success,error)
        function success(response) {
            console.log(response,"I got the data i requested")
            $scope.Contactlist=response.data;
        }

        function error(response){
            alert("Please check your code");
        }
    };
    refresh();
    $scope.addContact=function () {
        console.log($scope.contact);
        $http.post('/Contactlist',$scope.contact).then(success,error)
        function success(response) {
            console.log(response);
            $scope.Contactlist=response.data;
            refresh();
        };
        function error() {
            alert('error occured');
        }
        $scope.contact =null;
    }
    $scope.remove=function (id) {
        console.log(id);
        $http.delete('/contactlist/',id);
        refresh();
    }
}]);

2 Answers2

1

I think you should try calling it like this

 $http.delete('/contactlist/'+id);
Asif Saeed
  • 1,985
  • 14
  • 24
0

if you are getting ID here

var id= req.params.id;
console.log(id);

then simply run query

db.contactlist.remove({_id:id},function (err,doc) {
    if(err){ console.log("err n remove")}
else{console.log("Remove Success")}
})
Vipul Pandey
  • 1,507
  • 11
  • 20