流星:“模板助手中的例外”(Meteor: “exception in template helper”)

编程入门 行业动态 更新时间:2024-10-25 00:32:33
流星:“模板助手中的例外”(Meteor: “exception in template helper”)

我有一个基本的帖子流在Meteor中运行,从Posts集合中提取。 该模板由以下模板助手提供,该助手可以进入集合:

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

字符6:41对应于loggedUser._id字符串的中间位置。 这是怎么回事?

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?

最满意答案

当第一次运行你的应用程序时,助手将被执行, Meteor.user()返回null因为登录恢复过程需要几毫秒。

你需要一个警卫来防止访问loggedUser._id ,否则你会得到一个异常。

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

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

更多推荐

本文发布于:2023-07-24 10:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流星   助手   模板   Meteor   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!