2016-03-05 78 views
0

以下代碼按預期工作,即助手在有文檔時返回true,如果沒有文檔則返回false。但是,我在控制檯上發出警告。流星助手 - 檢查文檔是否存在的函數

「錯誤:異常模板幫手: 類型錯誤:無法讀取的Object.Template.navigation.helpers.ichk未定義 財產‘filepickerId’......」

的警告是不一致的,不知道爲什麼我的情況。然而,代碼再次沒有任何流量,我可以告訴。

Template.nav.helpers({ 

     'ichk': function(){ 

      var ct= Info.findOne({_id:Meteor.userId()}); 

      if (ct.profilepic.filepickerId) { 
       return true; 
      }else{ 
       return false; 
      } 

回答

1

如果它工作,您應該添加一行以擺脫異常。

Template.nav.helpers({ 

     'ichk': function(){ 

     var ct= Info.findOne({_id:Meteor.userId()}); 

       if(ct){ 

       if (ct.profilepic.filepickerId) { 

       return true; 

       } 

       else{ 

       return false; 

       }} 

這樣你首先檢查文件是否存在。

+0

謝謝。有用!!! – user1487244

2

您需要一個guard。你的幫手可以這樣改寫:

Template.nav.helpers({ 
    ichk: function() { 
    var ct = Info.findOne({ _id: Meteor.userId() }); 
    return !!(ct && ct.profilepic && ct.profilepic.filepickerId); 
    } 
} 
+0

謝謝,這個作品呢! – user1487244