我使用 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
(什么都没找到)。您无法访问 null
的 mode
属性。
关于javascript - 在 Meteor 中使用全局助手的模板助手中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674659/