2016-03-05 45 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