AngularJS CRUD - export.update()没有在服务器控制器中调用

时间:2015-06-21 14:41:34

标签: angularjs angularjs-routing

我使用以下Yeoman完整堆栈AngularJS NPM:generator-angular-fullstack

从客户端控制器调用update时,收到以下错误:Error: undefined is not a function (evaluating 'User.update')我希望在Web检查器日志中看到以下内容:

'5586c4e7214a22b5efbd1672'
'updateUser Called'  <-- Never routes to server controller

服务器/ API /路线:

//Tried PATCH and PUT
router.patch('/:id', auth.isAuthenticated(), controller.update);
//router.put('/:id', auth.isAuthenticated(), controller.update);

服务器/ API /控制器:

exports.update = function(req, res, next) {  
    console.log('updateUser Called');   
};

客户端/应用程序/控制器:

 'use strict';

 angular.module('demoApp')
 .controller('SandboxCtrl', function ($scope, $http, $location, Auth, User)       {

 $scope.getCurrentUser = Auth.getCurrentUser;

 $scope.user = {};
 $scope.profiles = {};
 $scope.allergens = {};

 $http.get('/api/users/me').success(function (user) {
  $scope.user = user;
  $scope.profiles = user.profiles;

  console.log(user.name);
  console.log(user.profiles);
 });

 // Update existing User
 $scope.update = function () {
  var user = $scope.user;

  console.log(user._id);

  User.update(function () {
     $location.path('/' + user._id);
  }, function (errorResponse) {
        $scope.error = errorResponse.data.message;
     });
   };
});

/客户端/用户/工厂:

'use strict';

 angular.module('demoApp')
   .factory('User', function ($resource) {
     return $resource('/api/users/:id/:controller', {
       id: '@_id'
 },
 {
  changePassword: {
    method: 'PUT',
    params: {
      controller:'password'
    }
  },
  update: {   //<-- I was missing this! 
    method: 'PATCH'        
  },
  get: {
    method: 'GET',
    params: {
      id:'me'
    }
   }
   });
 });

1 个答案:

答案 0 :(得分:0)

在AngularJS NPM generator-angular-fullstack中,工厂/服务隐藏在/client/components/auth/user.service.js

为现有工厂添加了必要的对象句柄解决了这个问题。

update: {   //<-- I was missing this! 
method: 'PATCH'        
},