流星:“模板帮助器中的例外". [英] Meteor: "exception in template helper"

查看:61
本文介绍了流星:“模板帮助器中的例外".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Meteor中运行的基本帖子流,它是从Posts集合中提取的.模板由到达集合中的以下模板帮助器提供:

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

    });

该助手似乎一切正常,并且帖子按预期方式显示.但是,我在控制台中收到这个模糊的错误,无法解决该错误: Exception in template helper: postsStream@http://localhost:3000/client/views/stream/post-stream.js?37c902af708ff817888efc24c4e45f352cfb6884:6:41

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

字符6:41对应于loggedUser._id字符串的中途.发生了什么事?

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

推荐答案

在首次运行您的应用程序时,由于登录恢复过程需要几毫秒的时间,因此将使用Meteor.user()返回null的方式来执行帮助程序.

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

您需要一个防护措施来阻止对loggedUser._id的访问,否则会出现异常.

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

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

这篇关于流星:“模板帮助器中的例外".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆