What the proper way to avoid console warning, when meteor collection is not ready

41 views Asked by At

Every time I refresh the page I receive the following console warning for every single helper that is returning something to template from collection. I know the reason is because the subscription is not ready yet, but what is the solution?

Exception in template helper: TypeError: Cannot read property 'x' of undefined.

I'm already using if(collection.find({}) !== undefined) , but this makes my codes so messy, there must be a way to fix this issue. then I tried guards and still not 100% solved.

2

There are 2 answers

0
Brendan Turner On

Meteor.subscribe returns a handle with a reactive method called .ready(). You can use that in your helper to only return the mongo cursor once it's ready.

Edit: docs

1
coagmano On

In addition to Brendan's answer, using Blaze you can check if the subscriptions for the template is ready using

this.subscriptionsReady()

Which checks all the subscriptions scoped to the template with

this.subscribe()

in your onCreated or onRendered blocks