Jack Reid
Jack Reid

Reputation: 87

Meteor: "exception in template helper"

I have a basic post stream running in Meteor that pulls from the Posts collection. The template is feed by the following template helper that reaches into the collection:

    Template.postStream.helpers({
        /* Supplies posts collection to the client
        *  view. */
        postsStream: function(){
            var loggedUser = Meteor.user();
            return Posts.find({ userID: loggedUser._id });
        },

    });

The helper seems to all work fine and the posts appear as expected. However, I get this vague error in the console and I can't work out how to clear it: Exception in template helper: postsStream@http://localhost:3000/client/views/stream/post-stream.js?37c902af708ff817888efc24c4e45f352cfb6884:6:41

Character 6:41 corresponds to midway through the loggedUser._id string. What's going on?

Upvotes: 3

Views: 944

Answers (1)

saimeunt
saimeunt

Reputation: 22696

When first running your application, the helper will get executed with Meteor.user() returning null because the login resume process takes a few milliseconds.

You need a guard to prevent access to loggedUser._id, otherwise you'll get an exception.

return Posts.find({ userID: loggedUser && loggedUser._id });

Upvotes: 6

Related Questions