【问题标题】:AngularJS CRUD - export.update() not calling in server controllerAngularJS CRUD - export.update() 没有在服务器控制器中调用
【发布时间】:2015-06-21 14:41:33
【问题描述】:

我正在使用以下 Yeoman 全栈 AngularJS NPM:generator-angular-fullstack

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

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

【问题讨论】:

  • 你在哪里定义用户?好像没有update功能?
  • 更新了客户端控制器以显示用户在哪里定义。
  • 您只显示了对 User 的引用,而不是 factory/service 函数
  • 是的..你也可以提供服务用户
  • 工厂更新并发布,记录错误消失了!但我仍然没有收到 Logger 消息:'updateUser called'。

标签: angularjs angularjs-routing


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 2017-10-11
    • 1970-01-01
    • 2014-01-04
    • 2018-09-29
    • 2015-05-07
    • 2014-02-11
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多