Reputation: 97
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?
Edit: can this be extended to the object property:
query = 'Berlin',
filter = 'City',
queryObject = {filter: {$contains: query}};
Upvotes: -1
Views: 271
Reputation: 331
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};
Upvotes: 0
Reputation: 193311
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.
Upvotes: 0