Search code examples
javascriptjsonobjectbackbone.jsquery-engine

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

Solution

  • 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.