【问题标题】:Exception in template helper using global helper in Meteor在 Meteor 中使用全局帮助器的模板帮助器中的异常
【发布时间】:2014-09-05 05:10:40
【问题描述】:

我用 UI.registerHelper 添加了一个全局帮助函数,它返回一个字符串。如果我访问特定站点,我可以看到正确的输出,但我得到以下异常:

Exception in template helper: http://localhost:3000/client/helpers/global_helpers.js?c1af37eca945292843a79e68a3037c17a0cfc841:18:45
http://localhost:3000/packages/blaze.js?cf9aea283fb9b9d61971a3b466bff429f9d66d7d:2458:21

这是我的代码:

UI.registerHelper('levelMode', function() {
    return Games.findOne({_id: this._id}).mode ? 'Difficult' : 'Easy';
});

任何想法如何解决这个问题?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    尝试添加一些检查:

    UI.registerHelper('levelMode', function() {
      if (typeof Games !== 'undefined' && Games != null)
        var game = Games.findOne({_id: this._id});
        if (game != null && game.mode)
          return 'Difficult';
      return 'Easy';
    });
    

    我的直觉是,错误源于尚未定义 Games(模板在定义集合之前呈现)或 findOne 返回 null(未找到)的情况。您无法访问 nullmode 属性。

    【讨论】:

    • 谢谢!那完成了工作。你认为我得到了正确的输出,因为页面被渲染了两次?
    • 它可能在填充Games 之前渲染一次,然后再次渲染一次Games.findOne 返回一个文档。查看 Iron Router 和 waitOn 以避免不必要的首次渲染。
    猜你喜欢
    • 2014-09-11
    • 2015-07-02
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2016-01-06
    • 2010-11-15
    相关资源
    最近更新 更多