Restangular has been throwing me a 404 when sending PUT requests, and I'm not sure why.
I have an Events collection, and I'm getting a single event and updating it in a form.
According to the Restangular docs, when I make a PUT request on the Event, it should route to api/events/:eventId
. Instead, I'm getting an error saying it's routing to api/events
.
The error:
PUT http://localhost:9000/api/events 404 (Not Found)
Can you see where I'm going wrong? I haven't changed the config on Restangular at all.
Here's my controller:
var restEvent = Restangular.one("api/events", $routeParams.eventId);
restEvent.get().then(function(eventData) {
// the eventData is successfully coming through
$scope.thisEvent = Restangular.copy(eventData);
// $scope.thisEvent is modified in the form
// updateEvent() runs when my Event form is submitted
$scope.updateEvent = function() {
$scope.thisEvent.put();
};
});
Here's my Express update routes:
// update route -------------
app.route('/api/events/:event_id')
.get(events.show)
.put(events.updateEvent);
Any suggestions?
This is actually an issue with Restangular/Mongo. Restangular defaults its primary key to "id", whereas Mongo uses "_id". When Restangular tries to append "id" from the Event object to api/events
, it can't find "id" and only routes to api/events
.
Here's the code that the Restangular docs suggest, and that solves this issue:
app.config(function(RestangularProvider) {
RestangularProvider.setRestangularFields({
id: "_id"
});
});