【问题标题】:RESTful CRUD for Angular and Express $resource用于 Angular 和 Express $resource 的 RESTful CRUD
【发布时间】:2014-07-11 21:32:08
【问题描述】:

我的 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')
    }
}]);

我的角度控制器:

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 }

【问题讨论】:

  • 你能发一下你的models/healingcenter-model.js'
  • 我已将模型添加到问题中。

标签: node.js angularjs rest express mean-stack


【解决方案1】:

在服务器端尝试(我假设您使用的是 moongose):

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

在客户端:

角度控制器:

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

编辑: 您可以直接在控制器中使用资源,也可以像在您的情况下那样只使用服务,这完全没问题。

【讨论】:

  • 由于某种原因,我在控制台中返回错误“DELETE localhost:3000/api/hc 404(未找到)”。
  • 确保您的 $resource url 与您在 express 中的路由中定义的端点匹配
  • 他们现在正在匹配,断开连接可能来自我的 mongodb 生成的“_id”而不是“id”。
  • 我已添加我的 JSON 结构以供参考。 '@tId' 对我来说是新的,它代表什么?
  • @tId 只是本地资源的一个参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 2021-08-27
相关资源
最近更新 更多