$ https delete在nodejs和mongodb中不起作用,无法删除

时间:2017-02-11 14:17:20

标签: angularjs node.js mongodb mongojs

我正在进入控制器,但是它进入服务器,当我点击删除时,我在控制台中获取了id。它的显示错误"可能是未处理的拒绝"和错误404

这是我的服务器: -

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

这是我的控制器

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 个答案:

答案 0 :(得分:1)

我认为你应该尝试这样称呼它

 $http.delete('/contactlist/'+id);

答案 1 :(得分:0)

如果你在这里获得身份证件

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

然后只需运行查询

db.contactlist.remove({_id:id},function (err,doc) {
    if(err){ console.log("err n remove")}
else{console.log("Remove Success")}
})