javascript - Meteor: Best Method for Passing Mongo Selector from Client to Server -


i have mongo collection following (foo(x) == keys; bars == values): edit- come relational database background. collection doesn't below, idea...

+--------+--------+--------+ |  foo1  |  foo2  |  foo3  | +--------+--------+--------+ | barbar | barbar |   bar  | |   bar  |  bar   | barbar | |   bar  | barbar | barbar | |   ...  |   ...  |   ...  | 

it's important me allow client filter data. sometimes, columns have filter, other times no columns have filter , anywhere in between. currently, i'm handling issue follows:

client

var afoo1filter = ["bar"] var afoo2filter = ["barbar", "barbar"] var afoo3filter = ["barbar", "bar"] //where user can affect array through other code 

server

meteor.publish("foos", function (afoo1filter, afoo2filter, afoo3filter ) {   return foocl.find({foo1: {$in: afoo1filter},                      foo2: {$in: afoo2filter},                      foo3: {$in: afoo3filter}},                      {limit: 10}); }); 

i'm hoping simplify passing through 1 object or 1 string client server, neither attempts have worked. see attempts, below:

attempt #1 - passing through string

client

var sfilter = "foo1: {$in: [\"bar\"]},                foo2: {$in: [\"barbar\", \"barbar\"]},                foo3: {$in: [\"barbar\", \"bar\"]}" 

server

meteor.publish("foos", function (sfilter) {   return foocl.find({sfilter},                      {limit: 10}); }); 

//////////////////

attempt #2 - passing through object

client

var ofilter = {   foo1: "bar" } 

server

meteor.publish("foos", function (ofilter) {   return foocl.find({ofilter},                      {limit: 10}); }); 

i don't have machine me @ moment, can't provide more detail on type of error thrown. have more info tonight. help!

the easiest way solve problem subscribe using selector:

client

var selector = {foo1: {$in: afoo1filter}, foo2: {$in: afoo2filter}}; meteor.subscribe('foos', selector); 

server

meteor.publish('foos', function (selector) {   return foocl.find(selector, {limit: 10}); }); 

however, it's important recognize gives client ability request any documents foocl collection wants. improved solution limit can requested using match on selector. example:

meteor.publish('foos', function(selector) {   check(selector, {     foo1: match.optional({$in: [string]}),     foo2: match.optional({$in: [string]})   });    if (!_.isempty(selector)) {     return foocl.find(selector, {limit: 10});   } }); 

this ensure selector conforms acceptable pattern before documents sent client.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -