[英]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
(沒有找到)。 您無法訪問null
的mode
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.