2015-06-21 61 views
0

我用以下约曼完整的堆栈AngularJS NPM:generator-angular-fullstackAngularJS CRUD - export.update()不调用服务器控制器

当从客户端控制器调用update,我收到以下错误:Error: undefined is not a function (evaluating 'User.update')我米期待看到我的网络督察日志如下:

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

你在哪里定义** User **?它似乎没有**更新**功能? – Michael

+0

已更新客户端控制器以显示用户(如果已定义)。 – CampbellGolf

+0

你只是显示**用户**的参考,而不是**工厂/服务**功能 – Michael

回答

0

在AngularJS NPM发电机角fullstack,工厂/服务隐藏在/client/components/auth/user.service.js下

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

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