【问题标题】:Meteor: Exception in Template Helper not going awayMeteor:模板助手中的异常不会消失
【发布时间】:2015-08-27 15:19:31
【问题描述】:

我的应用程序边栏中出现以下错误。

模板助手中的异常:TypeError:无法读取未定义的属性“org” 在 Object.Template.sidebar.helpers.orgUsers (http://localhost:3000/client/templates/includes/sidebar.js?85f4645305c91378ab2c0648488f2250753e8c70:3:29) 在 bindDataContext (http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:2786:16) 在 Blaze._wrapCatchingExceptions (http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:1607:16) 在http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:2834:66 在 Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:3382:12) 在 wrapHelper (http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:2833:27) 在 Object.Spacebars.call (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:18) 在http://localhost:3000/client/templates/includes/template.sidebar.js?85456dfc1ce0f189b2d312b2422db8a2e35f337e:15:22 在空。 (http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:2583:27) 在http://localhost:3000/packages/blaze.js?4e49999979a58da0e2265f7bd3f5910f9901b07b:1821:18

侧边栏应显示特定于组织(anization)的用户列表。发送用户的助手如下:

Template.sidebar.helpers({
  orgUsers : function() {
    var org_id = Meteor.user().org._id;

    var users = Meteor.users.find({
        "org._id" : org_id
    });
    return users;
  }
});

我在这里发布 user.org:

Meteor.publish("userData", function() {
    if (this.userId) {
        return Meteor.users.find({_id: this.userId},
            {fields: {'org._id': 1, 'org.active' : 1, 'emails.[0].address': 1, 'profile.name': 1, 'createdAt':1}});
    } else {
        this.ready();
    }   
});

目前我正在使用 Iron Router 将其发布到每个页面。看起来助手运行了几次,前几次它没有 org_id 也没有 org._id,但它最终得到并显示。不过,每次页面刷新时,我都会收到这个令人讨厌的控制台错误。非常感谢任何帮助。


路由器:

Router.configure({
layoutTemplate : 'layout',
loadingTemplate : 'loading',
notFoundTemplate : 'notFound',
waitOn : function() {
    return [ 
        Meteor.subscribe('orgUsers'),
        Meteor.subscribe('appointments'), 
        Meteor.subscribe('allServicesData'), 
        Meteor.subscribe('allOrgsData'), 
        Meteor.subscribe('userData'), 

    ];
}

});

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    您的助手在用户对象可用之前运行。您可以让 Iron-router waitOn 订阅(推荐的方法)到 userData,或者您可以防御性地编写您的助手:

    Template.sidebar.helpers({
      orgUsers : function() {
        if ( Meteor.user() ){
          var org_id = Meteor.user().org._id;
    
          var users = Meteor.users.find({
            "org._id" : org_id
          });
          return users;
        }
      }
    });
    

    【讨论】:

    • 感谢您的快速回复!所以订阅已经waitOn了。我将编辑问题以显示路由器。我还尝试了防御性帮助代码,它仍在发生。
    • 您正在执行Meteor.user().org._id 并获得Cannot read property 'org' of undefined,这意味着Meteor.user() 在代码运行时不存在。如果您刚刚检查过,应该可以!
    猜你喜欢
    • 1970-01-01
    • 2018-09-22
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2015-09-20
    • 2016-10-25
    相关资源
    最近更新 更多