-1

i just started using https://github.com/bevry/query-engine

i would like to know how i could create querys with dynamic data inside.

here a code example:

var query = "Berlin";
queryObject = '{"City":{$contains: "'+query+'"}}';
queryCollection.query(queryObject);

//TypeError: Object.keys called on non-object

queryObject = {"City":{$contains: "Berlin"}} ; 
queryCollectionquery.(queryObject);
//working as expected

Any Ideas?

dfsq is right

Edit: can this be extended to the object property:

query = 'Berlin', 
filter = 'City', 
queryObject = {filter: {$contains: query}};
Community
  • 1
  • 1
Paul-Siteway
  • 87
  • 1
  • 10

2 Answers2

0

Well queryobject has to be an object, not string. Try this:

var query = "Berlin",
queryObject = {City: {$contains: query}};

Note: quotes around object keys usually are not necessary.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • of course this is correct. is it possible to make do the same with the property like this: query = 'Berlin', filter = 'City', queryObject = {filter: {$contains: query}}; – Paul-Siteway Mar 22 '13 at 16:59
0

I think this is what you are looking for. Since you would like to set the key with a variable you need to use bracket notation; if you use dot notation the filter will be set to 'filter' and not the string stored in the variable.

var filter = 'City';
var query  = 'Berlin';

var queryObject = {};
queryObject[filter] = {$contains: query};
Alex
  • 331
  • 1
  • 2
  • 9