在angularjs 1.0.8不起作用PUT休息服务

时间:2013-11-16 13:24:20

标签: javascript node.js rest angularjs express

我有angularjs应用程序,它有CRUD Rest服务。创建,读取和删除方法效果很好,但PUT不起作用。 我在Stackoverflow上发现并且接受的答案存在同样的问题,例如:

Angular JS: Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?

AngularJS - PUT method not working (404 error)

所以我喜欢这个答案,但我的更新(通过PUT方法)不起作用(状态代码:404'不能PUT / api / adverts')。 也许大家会注意到我的代码。一个重要的是我使用 angular 1.0.8

services.js(angularjs):

var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);

motoAdsServices.factory('Advert', ['$resource', function($resource) {
    return $resource('/api/adverts/:advertId', {}, {
       update: {method:'PUT', params: {advertId: '@advertId'}}
    });
  }]);

editAdvert.html

  <label class="control-label">Brand</label>
  <div class="controls">
    <select name="brand" ng-model="editAdvert.brand" required ng-options="b.name for b in brands">
      <option value=""></option>
    </select>
  </div>

  <label class="control-label">Model</label>
  <div class="controls">
    <select name="model" ng-model="editAdvert.model" required ng-options="m.name for m in editAdvert.brand.models">
      <option value=""></option>
    </select>
  </div>

controllers.js(angular.js - 我没有删除重要代码):

$scope.updateAdvert = function() {
  var editAdvert = {
    _id: $scope.editAdvert._id,
    brandName: $scope.editAdvert.brand.name,
    modelName: $scope.editAdvert.model.name,
    year: $scope.editAdvert.year,
    price: $scope.editAdvert.price,
    imageUrl: $scope.editAdvert.imageUrl,
    countryName: $scope.editAdvert.country.name,
    regionName: $scope.editAdvert.region.name
  };

  // !!! I TRY THIS TO FIX !!!
  $scope.advertId = $scope.editAdvert._id;

  // !!! THIS NOT WORKS STILL !!!
  Advert.update(editAdvert, function() {
    previousAdvert = angular.copy($scope.editAdvert);
    alert('Advert updated');
  });
};

server.js(nodejs):

var express = require('express');
var path = require('path');
var http = require('http');
var adverts = require('./routes/adverts');

var app = express();

app.configure(function() {
  app.set('port', process.env.PORT || 3000);
  app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/api/adverts', adverts.findAll);
app.get('/api/adverts/:id', adverts.findById);
app.post('/api/adverts', adverts.add);
app.put('/api/adverts/:id', adverts.update);
app.delete('/api/adverts/:id', adverts.remove);

http.createServer(app).listen(app.get('port'), function() {
  console.log("Express server listening on port " + app.get('port'));
});

作为回应,我得到404和'不能PUT / api / adverts'。 我几乎确信这是URL末尾缺少“_id”的问题。 看图: enter image description here

请查看我的代码并提出一些建议。

1 个答案:

答案 0 :(得分:3)

我在这里看到一些不一致的地方。首先,update应该是实例方法。所以调用应该是这样的:$scope.editAdvert.$update(...而不是Advert.update($scope.editAdvert...

这里的另一件事是参数名称的不同。指示资源,资源的 id 应为advertId,而如图所示,该对象的id为_id

最后,您正在设置$scope.advertId = $scope.editAdvert._id; ......正如您所说的那样。问题是,它与对象$scope.editAdvert无关......它只是$ scope的另一个对象。资源不知道它。

查看有关ngResource here

的更多详情