Meteor.publish loads once and won't take new query

Hi there ! I’m a meteor newbie and I need some insight on what I’m doing wrong here.

I’ll explain what the app does and then post the code.

First, we input an address that is geocoded in a method. The result is then sent through a subscription to a publication.

In the publication, it finds all the office branches, sorted from nearest to farthest.

The result is sent to a reactivevar and to the template.

While in the page, if I make another input, nothing happens. In the publication, if I put a console.log at the end, I can see that the first request will fire the publication but then it won’t. If an error is thrown out (from methods.js), the list will dissapear and I can make another search. So I suspect that the subscription needs to stop().

Edit : I removed the code since it was completely changed thanks to waldgeist and I don’t people to misuse the bad code.

Your code is quite complicated. Is there a reason why are not just using one single method for retrieving the branches and sending them back to the client? Using a pub/sub seems to be overkill for me and requires server-side resources. Plus, your code would be much shorter.

I don’t need a sub / pub to sort specific items from a mongoDB ?

Edit : To specify, the branch offices are stored in a mongo collection. They are organized with SimpleSchema.

So I should pull them out and then select them in a single method, then put the IDs in a ReactiveVar ? Something like that ?

As far as I understand your use-case, the branches are already stored in the DB right? So you can do the request to the DB and return the result using the same method.

Regarding the result: You don’t have to return just the IDs alone. You can also return all branches you retrieved, e.g. in an array sorted by distance to your reference address. To save network bandwidth, I would limit the number of fields you retrieve from the database, so your array stays compact.

You can either store the retrieved values in a ReactiveVar, or you setup a local Mongo Collection on the client side, based on the array you have retrieved from the server side. Local collections are described here: The advantage of this approach is that you can perform database operations on this, including reactivity. The downside is that they don’t support geospatial queries, IIRC. So in your use-case, it might be better to sort the data according to geospatial information on the server side and return it in a pre-sorted manner. Or, you store the distance in the returned fields and store them in the local collection.

Yes the branch offices are already stored. The user will locate himself and (eventually) either find the nearest offices, all the offices in their city or their region.

For now, I’m trying to list branch offices in order of geospacial distance and then once they are all stored, I will use google driving distance services and other services. This can be done client side and saved later tho.

I will fork my progress and rework it in a single method. I’ll keep you posted. Thanks for the input !

1 Like