2014-07-10 110 views
0

我有我的JSON查詢和創建正確。我有點卡住如何從服務器中刪除項目。他們正在刪除的角度,但我似乎無法得到連接的權利,刪除他們在服務器上。

我server.js:

var hcController = require('./server/controllers/services-controller.js') 

//REST API 
app.get('/api/hc', hcController.list); 
app.post('/api/hc', hcController.create); 
app.delete('/api/hc:_id', hcController.delete); 

我的服務器端模型

var mongoose = require('mongoose'); 
    module.exports = mongoose.model('HealingCenterData',{ 
     title: String, 
     shortname: String, 
     summary: String, 
     description: String 
    }); 

我的服務器側控制器

var Hc = require('../models/healingcenter-model.js') 

module.exports.create = function (req, res) { 
    var hc = new Hc(req.body); 
    hc.save(function (err, result){ 
    res.json(result); 
    }); 
} 

module.exports.list = function (req,res) { 
    Hc.find({}, function (err, results){ 
    res.json(results); 
    }); 
} 

module.exports.delete = function (req, res) { 
    ??????? 
    }); 
} 

我的角服務:

app.factory("HC", ["$resource", function($resource) { 
    return { 
     API: $resource('/api/hc/:id') 
    } 
}]); 

我的角度控制器:在客戶端

exports.delete = function(req,res){ 
    if(req.params.id !==null || req.params.id!==undefined){ 
     Hc.remove({_id:req.params.id},function(err){ 
       res.send(200); 
     }); 
    } 
}; 

app.controller('servicesController', ['$scope', 'HC','$resource', function ($scope, HC, $resource) { 

    HC.API.query(function(results) { 
     $scope.services = results; 
    }); 

    $scope.createService = function() { 
     var service = new HC.API(); 
     service.title = $scope.serviceTitle; 
     service.shortname = $scope.serviceShortname; 
     service.summary = $scope.serviceSummary; 
     service.description = $scope.serviceDescription; 
     service.$save(function(result){ 
     $scope.services.push(result); 
     $scope.serviceTitle = ''; 
     $scope.serviceShortname = ''; 
     $scope.serviceSummary = ''; 
     $scope.serviceDescription = ''; 
     }); 
    } 


$scope.removeItem = function(index){ 
    $scope.services.splice(index, 1); 
    } 

}]); 

我的JSON結構

{ "_id" : ObjectId("53bea9366a03a66c2dad68bb"), "title" : "Auto Clinic", "shortname" : "auto_clinic", "summary" : "Volunteers evaluate car problems and make minor repairs. Labor is free, and the car owner pays for any needed parts. Oil changes are performed at a reduced cost. All services are performed on Saturdays.", "description" : "No additional information yet.", "__v" : 0 } 
+0

你可以發佈你的'模型/ healingcenter-model.js'' – tymeJV

+0

我已經添加了模型的問題。 – byrdr

回答

1

在服務器端嘗試(我假設你正在使用moongose) :

角度控制器:

var endPoint = $resource('/api/hc/:id', {id:'@tId'}); 
$scope.removeItem = function(id){ 
var ep = new endPoint({tId:id}); 
    ep.$delete(function(res){ 
     //TODO: update local array in scope 
    }); 
}; 

編輯: 你可以在控制器或者就像你在你的案件已經完成了服務直接使用的資源,這是完全罰款。

+0

出於某種原因,我在控制檯中返回錯誤「DELETE http:// localhost:3000/api/hc 404(Not Found)」。 – byrdr

+0

@byrdr將參數從端點_id更改爲ID –

+0

請確保您的$資源url匹配您的路由中定義的端點快遞 –