angular-meteor icon indicating copy to clipboard operation
angular-meteor copied to clipboard

Passing selectors from client for inexpensive DB querying

Open AnjirHossain opened this issue 11 years ago • 6 comments

I'm trying to specify selectors for the meteor collection.find in order to implement search functionality in my app. I know 'how to' questions are best for SO but I got nowhere there.

1st attempt:

// inside on of the controllers (Client)

var selectors = {"make.name":"Toyota"}
var test = Listings.find(selectors);

var test2 = Listings.findOne(selectors);
console.log(test); // undefined for obvious reasons
console.log(test2);// logs only one listing

2nd attempt:

Listing = new Mongo.Collection('listings');

// Server
Meteor.publish('listings', function(selectors) {
    if (selectors) {
        return Listings.find(selectors);
    } 
    return Listings.find();
});

// Client 
var test = $meteor.collection(Listings);
var selectors = {"make.name":"Toyota"}
$meteor.subscribe("listings", selectors);

console.log(test);  // logs everything in db instead of just listings with property "make.name" = "Toyota"

I just want to be able to pass the damn selectors to the server. :sob:

AnjirHossain avatar Aug 27 '15 23:08 AnjirHossain

I think you flipped your logic. The server publish code should be if (selectors), or if (!!selectors).

Tallyb avatar Aug 28 '15 04:08 Tallyb

Thank you so much for the response! if (selectors) is actually what I have, it was a typo I made posting the issue. I'm still getting the entire db, could it be that I'm timing the $meteor.subscribe call incorrectly? or passing the selecters var in the wrong place? I'm able to get around this issue if I try something like Meteor.subscribe('listings', selectors) but this feels hacky, and it breaks all other parts of my app that use the 'listings' collection.

AnjirHossain avatar Aug 28 '15 05:08 AnjirHossain

I would suspect that there is another subscription which get the full listing, and in this case the collection gets everything. try and remove the return Listings.find(); If you want to further discuss it - I suggest to go on gitter.

Tallyb avatar Aug 28 '15 07:08 Tallyb

That ^ + $or syntax did the trick; thanks!

AnjirHossain avatar Aug 28 '15 07:08 AnjirHossain

Btw, be sure to check your selectors on the server, to avoid stuff like: http://blog.east5th.co/2015/08/10/dos-your-meteor-application-with-where/

sebakerckhof avatar Aug 31 '15 11:08 sebakerckhof

thanks everyone!

Urigo avatar Sep 01 '15 19:09 Urigo