angularjs-mongolab icon indicating copy to clipboard operation
angularjs-mongolab copied to clipboard

missing JSON.stringify in distinct function

Open jojosati opened this issue 11 years ago • 2 comments

missing JSON.stringify in distinct function at line 87

https://github.com/pkozlowski-opensource/angularjs-mongolab-promise/blob/master/src/mongolabResourceHttp.js#L87

  •   var httpPromise = $http.post(dbUrl + '/runCommand', angular.extend({}, queryJson || {}, {
    
  •  var httpPromise = $http.post(dbUrl + '/runCommand', angular.extend({}, JSON.stringify(queryJson || {}), {
    

jojosati avatar Jul 11 '13 16:07 jojosati

@jojosati hmm, I'm not sure converting JSON to string here is the best way to go as we really want to send JSON in the POST request as data payload, with proper request headers etc.

What is the actual problem you were facing? Is it about the fact how AngularJS stringifies properties where names start with $?

pkozlowski-opensource avatar Jul 27 '14 10:07 pkozlowski-opensource

@pkozlowski-opensource pardon me. It's long ago.

I think at that time If passing object data via $http.post, angular will use its own stringify function that strip out all $properties i.e. $or, $and, $in

I've not touched the mongoResource code since made it work and now wonder my final code is look like this

  Resource.runCommand = function(commandJson, successcb, errorcb) {
    var httpPromise = $http.post(
      dbUrl + '/runCommand', 
      JSON.stringify(commandJson), 
      {
        params: defaultParams,
        timeout: 5 * 60 * 1000 // server overload breaker 5 minutes

      });
    return promiseThen(httpPromise, successcb, errorcb, function(data) {
      return data.values;
    });
  };


  Resource.distinct = function(field, queryJson, successcb, errorcb) {
    return Resource.runCommand({
      distinct: collectionName,
      key: field,
      query: queryJson || {}
    }, successcb, errorcb);
  };

jojosati avatar Jul 27 '14 15:07 jojosati