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:
Here is
console.log($scope.user)
from the browser after I edited the form and hit Save (which callssaveUser
):{__v:0, _id:"50b1a966c12ef0c426000007", password:"NEW MODIFIED PASSWORD"}
And here's what Node logs as
console.log(req.user)
:{__v:0, _id:"50b1a966c12ef0c426000007", password:"originalPassword"})
req.user
is 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 a PUT
request, you're looking for data in req.body
.