繁体   English   中英

模板助手中使用Meteor中的全局助手的例外情况

[英]Exception in template helper using global helper in Meteor

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

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

我的预感是错误源于游戏尚未定义的情况(模板在定义集合之前呈现)或者findOne返回null (没有找到)。 您无法访问nullmode属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM