Meteor Publish/Subscribe passing object with string parameter issue


#1

I am trying to pass a object { key:value} and send it to meteor publish so i can query to database.

My Mongo db database has (relevant datas only) for products:

products : {
     categs:['Ladies Top','Gents'],
     name : Apple
}

In meteor Publish i have the following:

  Meteor.publish('product', (query) =>{
    return Clothings.find(query);
  })

In client i use the following to subscribe:

      let query = {categs:'/ladies top/i'}; // please notice the case is lower 
      let subscribe = Meteor.subscribe('product',query);   
      if (subscribe.ready()){
        clothings = Products.find(query).fetch().reverse();  
        let count = Products.find(query).fetch().reverse().length; // just for test 
      }

The issue is, when i send the query from client to server, it is automatically encoded eg:
{categs:'/ladies%top/i'}

This query doesnot seem to work at all. There are like total of more than 20,000 products and fetching all is not an option. So i am trying to fetch based on the category (roughly around 100 products each).

I am new to ,meteor and mongo db and was trying to follow existing code, however this doesnot seem to be correct. Is there a better way to improve the code and achieve the same ?

Any suggestion or idea is highly appreciated.

I did go through meteor docs but they dont seem to have examples for my scenario so i hope someone out there can help me :slight_smile: Cheers !


#2

What is not clear: Is your categs an array of strings or a string?

To make it clear what I’m asking for - is it:

products : {
     categs: [String],
     name: String
}

or is it:

products : {
     categs: String,
     name: String
}

with categs being one value out of several possibilities.


#3

Hey , the categs is an array of string. :slightly_smiling_face: categs:[“Ladies Top”,””Gents”]


#4

Ok, before we get to solving this, I just want to plead with you not to pass your client supplied query directly into mongo like this EVER.

Now that we have that out of the way. Probably the best way to go about this is to have the client send an array of categories and then use the $in operator.

Don’t forget to at least use check to validate your args. A schema with allowedValues would be ideal though.