[solved] How to pass a JSON with placeholder as parameter?


#1

Hi guys,

for me as a javascript newbie this is quite hard to explain…
so I am giving my best and hope that you guys can follow… :slight_smile:

I am searching for a way in javascript to dynamically build mongo-selectors.

In order to achieve this, I want to dynamically pass a “JSON configuration” as a parameter including a placeholder for the value.

For example dynamicallyBuildMyMongoSelector([surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}]) (WHICH does NOT work.)

Basically I want to pass a configuration-json to a function and then let it build a mongo-selector.

  // Take JSON configuration
  // ... fill theValuePlaceholder with real values
  // return array containing real mongo-selectors (with real values)
  dynamicallyBuildMyMongoSelector(configurationArray) {
    var theQuery = [];
    var exampleValue = 'example';

    configurationArray.forEach(function(element, index) {
      log(element.name);
      // get selector and replace "theValuePlaceholder" with a "real" value
      selectorWithValue = (element.selector, exampleValue); // HOW do I do this???
      theQuery.push(selectorWithValue);
    });

    return theQuery;
  }

  // call function an pass selector layout as parameter
  dynamicallyBuildMyMongoSelector(
    [
      // configure filters and set selector layout
      {
        name: 'filter-name',
        selector: {
          // PROBLEM: I am getting an error because "theValuePlaceholder" is NOT SET
          // ... I DO NOT want to set it here but have it here as placeholder
          $or: [
            {surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
            {forename: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
          ]
        }
      },
      {
        name: 'filter-country',
        selector: {
          country: theValuePlaceholder
        }
      }
    ]
  );

Any ideas?

Please ask if things are unclear… :smile:


#2

I find it a bit strange to pass your “template” to the function, but not the variable part.
You could pass your “template” as a JSON string and do a replace on the string with your value, but that’s a bit hacky.
Could you please share your use case for doing this ?


#3

Perhaps this code (far from perfect) which build a mongo update operation will give you ideas ?


#4

Hi Vincent @vjau, thanks for your quick response! Right now I think that I’ll try to hack this using JSON-to-string and string-to-JSON, but I’m not sure if it’ll work.

This is the use-case: I am trying to build a configurable filter for aldeed:tabular using Blaze Components.

I`d like to pass the blaze component a dynamic list of input-fields with specific mongo-selectors via it’s constructor.

The Blaze component shall then read the user-input from the associated template
and put together the final mongo-db query to show the filtered data within aldeed:tabular.
So the componant has to read the value from the user input
and replace theValuePlaceholder with the input-value.

OH YEAH!

Do you recon JSON stringify is the way to go?


#5

I think you could use ES6 template strings :

edit: but this is ugly
edit: forget it, it won’t work.


#6

@vjau: Thanks for your help!

I found a solution by doing something like:

dynamicallyBuildMyMongoSelector(
    [
      // configure filters and set selector layout
      {
        name: 'filter-name',
        selector: {
          // PROBLEM: I am getting an error because "theValuePlaceholder" is NOT SET
          // ... I DO NOT want to set it here but have it here as placeholder
          $or: [
            {surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
            {forename: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
          ]
        }
      },
      {
        name: 'filter-country',
        selector: {
          country: theValuePlaceholder
        }
      }
    ]
  );

… and then traversing thru the json-object and replacing the value.
I am using this library https://www.npmjs.com/package/traverse for traversing…