Exception in Template Helper using Global Helper in Meteor

I added a global helper function with UI.registerHelper that returns a string. If I access a specific 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 fix this problem?

+3


source to share


1 answer


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 guess is that the error stems from cases where games are not defined yet (template is rendering before collection definition) or findOne

returns null

(nothing found). You cannot access the property mode

null

.

+3


source







All Articles