Failed to convert complex javascript object to query string

Here is the javascript object I'm trying to convert to a query string

{$and: [{topic: categoryIds} , {$or :[ {'groups 1': {$ne: ''}}, {groups: $scope.myGroups}]}]}; 

Basically I am looking for a match with the topic categoryIds and grab that have an empty group array or that the group array has values ​​and matches one in the $ scope.mygroups array

My question is what would be the best way to convert this to an easily parsed format so that I can add it to the GET request and how you would parse it on the express server.

+6
source share
2 answers

I use this code to create requests for a server with a MongoDB server (using angular and lodash):

 .factory('mongoQuery', function() { return { fromJson: function(json) { return JSON.parse(json, fromJsonReviver); }, toJson: function(object) { return JSON.stringify(object, toJsonReplacer); } }; function fromJsonReviver(key, value) { var val = value; if (_.isPlainObject(value)) { if (_.isNumber(value.$date)) { val = new Date(0); val.setUTCMilliseconds(value.$date * 1000); } else if (_.isString(value.$regexp)) { var match = /^\/(.*)\/([gimy]*)$/.exec(value.$regexp); val = new RegExp(match[1], match[2]); } } return val; } function toJsonReplacer(key, value) { var val = value; if (_.isPlainObject(value)) { val = _.extend({}, value); for (var k in value) { val[k] = toJsonReplacer(k, val[k]); } } else if (_.isDate(value)) { val = {$date: (new Date(value)).valueOf() / 1000}; } else if (_.isRegExp(value)) { val = {$regexp: value.toString()}; } return val; } }) 

It includes many of the sentences mentioned by others in the comments, and supports dates and regular expressions.

Other than that, if you need to send a request using a GET request, just use encodeURIComponent , as others have mentioned.

Here is a working example: http://plnkr.co/edit/b9wJiUkrHMrDKWFC1Sdd?p=preview

0
source

What you think here basically is to rebuild the API server in conjunction with some mongodb database and query it in some format.

  • REST is the "best practice" you are looking for. This is a standard that encapsulates some common actions in http ressources.
  • You should know that you do not need to rebuild the ecosystem based on such a standard. There are full-featured REST API servers, some of which are even based on express.js. Loopback and sails.js . They provide some additional features, such as
    • Model abstraction through ORM and agnostism in the database
    • Automatic REST actions, from a database schema or model simulation
    • Advanced request through extended REST ("where", "limit", "order", ...)
    • In real time with web sites like REST
    • Client-side libraries that help you query the server
  • Some standalone exernal libraries such as js-data or Restangular can work very well with a REST server and act as an external backend connector

Now, if I were to fully answer your question, and if you really want to go with your solution, I will simply add the mongo request to the where request parameter to the http call with encodeURIComponent , as indicated earlier.

0
source

Source: https://habr.com/ru/post/974323/


All Articles