3

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?

jloosli
  • 2,461
  • 2
  • 22
  • 34

1 Answers1

3

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

michael
  • 16,221
  • 7
  • 55
  • 60
  • That is correct...it looks like the offending code is in the [angular source](https://github.com/angular/angular.js/blob/834d316829afdd0cc6a680f47d4c9b691edcc989/src/Angular.js#L908). Since it only strips this out on objects, if I stringify it myself with JSON.stringify() before adding it to the put request, it goes through without any problems. Thanks for pointing me in the right direction. – jloosli Jan 16 '14 at 21:03