Meteor: “exception in template helper”

你说的曾经没有我的故事 提交于 2019-12-10 19:04:15

问题


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?


回答1:


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 });


来源:https://stackoverflow.com/questions/30936603/meteor-exception-in-template-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!