Vue. How to subscribe passing arguments

In the doc it’s explained that when publishing you can receive parameters:

Meteor.publish('coins', (address) => {  
   console.log("publish address: ", address)

But I don’t know where to pass the address parameter when subscribing.

Doing this doesn’t pass the address param

meteor: {
     $subscribe: {
       'coins': [],
     },
     coins() {
       return Coin.find( { address: this.$route.params.id }, { } )
     },   
   },

Where, in the subscribe, can I pass the address parameter?

Thanks.

Assuming the address you want to send to the publication is retrieved from the route id parameter, you can use:

meteor: {
  $subscribe: {
    'coins': [ this.$route.params.id ],
    },
    coins() {
      return Coin.find( { address: this.$route.params.id }, { } )
    },
  },

I much prefer this syntax, which is also availabie in the vue-meteor-tracker npm package https://github.com/meteor-vue/vue-meteor-tracker#vue-integration-for-meteor

mounted () {
  // Subscribes to the 'threads' publication with two parameters
  this.$subscribe('thread', ['new', 10])
}
1 Like

I found the solution:

Meteor.publish('coins', function(address) { // process data according to address parameter
  return Coin.find({ address: address }, { fields: Coin.publicFields })
}

Then in the client:

meteor: {
    $subscribe: {
      'coins': function() {
        return [this.$route.params.id] // send the param to the server
      }
    },
    coins() {
      return Coin.find({}) // looks redundant but it's necessary. Tells the client to read all the documents sent by the server.
    }
  },

Pufffff.

It is not redundant. You should really use:

return Coin.find({ address: this.$route.params.id })

Without the find criteria you may retrieve more documents from the minimongo collection than you wished for. Remember you can have more than one subscription for the same collection active at any one time.

Read carefully this section on the user guide: Publications and Data Loading | Meteor Guide

2 Likes

To add on to that, start looking at your subscriptions as if they are clients fetching from endpoints. Minimongo is just the store where that data ends up. Multiple endpoints store documents in the same store.

Thanks both.

I will add the filter.

Cheers.