javascript - 在 Meteor 中使用全局助手的模板助手中的异常

标签 javascript meteor

我使用 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';
});

有什么办法可以解决这个问题吗?

最佳答案

尝试添加一些检查:

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 属性。

关于javascript - 在 Meteor 中使用全局助手的模板助手中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674659/

相关文章:

javascript - Jquery $.Ready 函数的最佳实践

mongodb - Mongo 按动态字段排序

meteor - 当订阅的 session 变量改变时,iron-router重新加载路由

javascript - CKeditor 不使用 Meteor 渲染

javascript - 不能使用命名空间 'Boom' 作为类型

javascript - 在 HTML5 视频播放时执行功能

javascript - 使用 for 循环时出现 javascript 错误,即使它正在工作

javascript - 使用 RequireJS 加载模块及其所有插件

javascript - Meteor 集合中的 2 个出版物良好实践

javascript - React router 在路由时最后追加页面