【问题标题】:Exception in template helper: TypeError: Cannot read property '0' of undefined模板助手中的异常:TypeError:无法读取未定义的属性“0”
【发布时间】:2017-08-29 03:47:00
【问题描述】:

当我开始遇到错误时,我正在使用流星中的用户帐户。控制台中打印的错误在我的 userEmail 辅助函数中。 我的问题是如果是 undefined 那么为什么我会得到输出?

我的html代码:

{{#each user}}
        {{#if student}}
            <div class="row">
                <div class="user-wrapper">
                    <div class="row">
                        <div class="name-wrapper col-md-3">
                            <span class="head">{{profile.firstname}}</span>
                        </div>
                        <div class="email-wrapper col-md-3">
                            <span class="head">{{userEmail}}</span>
                        </div>
                        <div class="email-wrapper col-md-3">
                            <span class="head">{{>starsRating mutable=true class="js-rate-images" id=_id}}</span>
                        </div>
                        <div class="email-wrapper col-md-3">
                            <span class="head">Remark</span>
                            <button class="btn btn-primary">Enter Remark</button>
                        </div>
                    </div>
                </div>
            </div>
    {{/if}}
    {{/each}}

我的辅助函数是:

Template.Users.helpers({
    user: function(){
        return Meteor.users.find();
    },
    userEmail:function(){
        return this.emails[0].address;
    },
    student:function(){
        return this.profile.profession === 'student';
    }
});

【问题讨论】:

  • emails 密钥默认不发布。您的用户出版物是什么样的?
  • 模板应该是return Meteor.user().emails[0]?虽然emails 甚至可能不存在,但如果您不使用accounts-password - 即accounts-facebook
  • 我正在使用账户密码

标签: javascript meteor meteor-useraccounts


【解决方案1】:

您的函数在加载集合之前正在运行,但由于 Meteor 助手是反应式的,一旦加载集合,您就会得到结果。
防止该消息的一种方法是有条件地运行您的代码:

if(this.emails !== undefined){
  return this.emails[0].address;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 2018-12-04
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多