Question

I'm using Restangular to connect with Mongolab. I'd like to use the following code to push an update:

var theData = {"$push":{exercises :{type: "running"}}};
Restangular.all('employees').one(user._id.$oid).customPUT(theData ,null, {apiKey: apiKey});

When I run this and look at the XHR request, the payload is always set to {}.

However, if I pull out the $, my payload looks like this:

{"push":{exercises :{type: "running"}}}

In that instance, the payload looks fine, but mongolab thinks I'm wanting to add a field named "push" instead of pushing to the excercises array since I'm not using the "$push" keyword.

I can have the "$" anywhere in the string except at the front (e.g " $push" and "push$" work) but unfortunately, that's what mongo requires in order to push an update. Is there some setting that I'm missing, or is this a bug in restangular?

Was it helpful?

Solution

yes, the $ will be striped: before the data is send, the data will be transformed with angular.toJson function:

@name angular.toJson

@function

@description

Serializes input into a JSON-formatted string. Properties with leading $ characters will be stripped since angular uses this notation internally.

If you don't want this behavior you have to provide a transformRequest function (http://docs.angularjs.org/api/ng.$http). If your data is already json you may just write:

transformRequest: function(data){
   return data;
}

the transformRequest must be provided as option during resource configuration. see
http://docs.angularjs.org/api/ngResource.$resource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top