Here’s how I defined my $resource:
app.factory('User', function($resource){
return $resource('/api/user', {}, {
get: { method:'GET'},
update: { method:'PUT'},
})
})
And here’s my controller:
function SettingsCtrl($scope, $http, User) {
$scope.user = User.get(
{}, //Params
function(data) { //Successfully received data
},
function(data) { //Failure to receive data
}
);
$scope.saveUser = function() {
$scope.user.$update();
console.log($scope.user);
};
}
The User.get() call at the top of the controller is correctly getting the data from the backend. In my HTML I’ve made a button that calls the $scope.saveUser function and created some inputs with ng-model, and it would appear that two-way data binding is working correctly. When I call console.log($scope.user) in the $scope.saveUser function, it returns the user object exactly as I expected, with the changes I made in my browser. However, on my Node backend, I log the object as it was received in the PUT request, and it does not reflect any changes I made in my browser, it looks identical to the original. After I click the button in my browser, the data is reset to its original value. What is going on? Why won’t it send the updated data that I’ve typed in my browser?
According to your comment:
req.useris usually an object attached to the request object to specify who the currently logged in user is, and is usually managed by middleware. Since you’re doing aPUTrequest, you’re looking for data inreq.body.