简体   繁体   English

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

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

I added a global helper function with UI.registerHelper which returns a String. 我添加了一个UI.registerHelper的全局辅助函数,它返回一个String。 If I access the particular site, I can see the correct output , but I get the following exception: 如果我访问特定站点,我可以看到正确的输出 ,但我得到以下异常:

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

This is my code: 这是我的代码:

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

Any ideas how to solve this issue? 任何想法如何解决这个问题?

Try adding some checks: 尝试添加一些检查:

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

My hunch is that the error stems from the cases where Games isn't yet defined (the template is rendering before the collection is defined) or findOne returns null (nothing found). 我的预感是错误源于游戏尚未定义的情况(模板在定义集合之前呈现)或者findOne返回null (没有找到)。 You can't access a mode property of null . 您无法访问nullmode属性。

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

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