2016-11-10 78 views
0

我試圖讓幫助者用逗號返回一個號碼(例如100000 = 100,000)。它正確返回,但我得到一個錯誤出現在我的控制檯。將逗號加上號碼時出錯

Exception in template helper: TypeError: Cannot read property 'toString' of undefined

如果我console.log(value);返回undefined

路徑:test.js

Template.registerHelper(
    'formatCurrency', function(value) { 
    return value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
    }, 
); 

路徑:test.html

{{formatCurrency compareSalary.userSalary}} 
{{formatCurrency compareSalary.min}} 

回答

1

在助手的常見故障的原因是數據不從你嘗試呈現它的時候訂閱了。這可以通過在返回之前測試該值來防止:

Template.registerHelper(
    'formatCurrency',(value)=>{ 
    return value && value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
    }, 
);