【发布时间】: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