2017-02-11 76 views
1

id我進入控制器,但其nt進入服務器,當我點擊刪除我在控制檯中得到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(); 
    } 
}]); 

回答

1

我想你應該嘗試調用它像這樣

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

@Dhiraj夏爾馬請更新是否有所幫助 –

0

如果你正在ID

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