Paul
Paul

Reputation: 97

dynamic backbone query with query-engine

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}};

Upvotes: -1

Views: 271

Answers (2)

Alex
Alex

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

dfsq
dfsq

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

Related Questions