【问题标题】:Exception in template helper: Cannot read property 'name' of undefined模板助手中的异常:无法读取未定义的属性“名称”
【发布时间】:2014-11-22 12:43:31
【问题描述】:

一段时间以来,我一直在努力为特定用户显示用户个人资料,但我取得了相当大的成功。我遇到标题错误的问题。

我发布这样的学校收藏

Meteor.publish("schoolData", function () {
  return Schools.find();
});

我的订阅看起来像这样

Meteor.subscribe('schoolData');

我的 HTML 看起来像这样

<template name="userProfile">
    <title>My Profile</title>
    <table>
            <tr>
                <td>User Name: </td>
            <td>{{userName}}</td>
        </tr>
        <tr>
            <td>First Name: </td>
            <td>{{firstName}}</td>
        </tr>
        <tr>
            <td>Last Name: </td>
            <td>{{lastName}}</td>
        </tr>
        <tr>
            <td>Registered E-mail: </td>
            <td>{{email}}</td>
        </tr>
        <tr>
            <td>Contact E-mail: </td>
            <td>{{email}}</td>
        </tr>
        <tr>
            <td>Phone Number: </td>
            <td>{{phoneNumber}}</td>
        </tr>
        <tr>
            <td>School: </td>
            <td>{{schoolName}}</td>
        </tr>
        <tr>
            <td>First year: </td>
            <td>{{firstSchoolYear}}</td>
        </tr>
        <tr>
            <td>Last year: </td>
            <td>{{lastSchoolYear}}</td>
        </tr>
        <tr>
            <td>Matriculated? </td>
            <td>{{matriculated}}</td>
        </tr>
        <tr>
            <td>House Name: </td>
            <td>{{houseName}}</td>
        </tr>
        <tr>
            <td>Country living in: </td>
            <td>{{country}}</td>
        </tr>
        <tr>
            <td>City living in: </td>
            <td>{{cityOfResidence}}</td>
        </tr>
        <tr>
            <td>Industry employed in: </td>
            <td>{{emplIndustry}}</td>
        </tr>
    </table>
</template>

而 javascript 看起来像这样

Template.userProfile.helpers({
    email: function() {return Meteor.user().emails[0].address},
    userName: function () {return Meteor.user().username},
    firstName: function () {return Meteor.user().profile.firstname},
    lastName: function () {return Meteor.user().profile.lastname},
    phoneNumber: function () {return Meteor.user().profile.phone},
    schoolName: function () {return     Schools.findOne(Meteor.user().profile.schoolName).name;},
    firstSchoolYear: function () {return Meteor.user().profile.firstschoolyear},
    lastSchoolYear: function () {return Meteor.user().profile.lastschoolyear},
    matriculated: function () {return Meteor.user().profile.matriculated},
    houseName: function () {return Meteor.user().profile.housename},
    country: function () {return Meteor.user().profile.country},
    cityOfResidence: function () {return Meteor.user().profile.cityofresidence},
    emplIndustry: function () {return Meteor.user().profile.emplindustry},
    signedUp: function () {return Meteor.user().profile.createdAt},
});

我似乎得到了错误 模板助手中的异常:TypeError:无法读取未定义的属性“名称” 它符合这是来自学校的收藏。 谁能帮我找出我的错误。

【问题讨论】:

    标签: meteor meteor-accounts


    【解决方案1】:

    首先,有更好的方法来做你想做的事——用 {{#with user}} 标签包装你的所有 html,如下所示:

    <template name="userProfile">
      {{#with user}}
        <title>My Profile</title>
        <table>
                <tr>
                    <td>User Name: </td>
                <td>{{userName}}</td>
            </tr>
            <tr>
                <td>First Name: </td>
                <td>{{firstName}}</td>
            </tr>
            <tr>
                <td>Last Name: </td>
                <td>{{lastName}}</td>
            </tr>
            <tr>
                <td>Registered E-mail: </td>
                <td>{{email}}</td>
            </tr>
            <tr>
                <td>Contact E-mail: </td>
                <td>{{email}}</td>
            </tr>
            <tr>
                <td>Phone Number: </td>
                <td>{{phoneNumber}}</td>
            </tr>
            <tr>
                <td>School: </td>
                <td>{{schoolName}}</td>
            </tr>
            <tr>
                <td>First year: </td>
                <td>{{firstSchoolYear}}</td>
            </tr>
            <tr>
                <td>Last year: </td>
                <td>{{lastSchoolYear}}</td>
            </tr>
            <tr>
                <td>Matriculated? </td>
                <td>{{matriculated}}</td>
            </tr>
            <tr>
                <td>House Name: </td>
                <td>{{houseName}}</td>
            </tr>
            <tr>
                <td>Country living in: </td>
                <td>{{country}}</td>
            </tr>
            <tr>
                <td>City living in: </td>
                <td>{{cityOfResidence}}</td>
            </tr>
            <tr>
                <td>Industry employed in: </td>
                <td>{{emplIndustry}}</td>
            </tr>
        </table>
    {{/with}}
    </template>
    

    还有像这样的助手:

    user: function(){return Meteor.users.findOne({_id:Meteor.userId()});
    

    现在您可以在 html 中使用用户集合中的所有数据,而无需帮助器

    对于您的问题 - 您的查询错误,它应该如下所示:

    schoolName: function () {
        return Schools.findOne({name:Meteor.user().profile.schoolName}).name;
    },
    

    我假设你想通过name查看学校,如果你在profile.schoolName中有_id,请将name更改为_id

    【讨论】:

    • 您好,感谢您的回答,我已按照您的建议对代码进行了更改,但仍然未定义为错误。我从浏览器的控制台运行查询并得到了这个响应 Schools.findOne({name:Meteor.user().profile.schoolName.name}); undefined 你认为这可能是权限问题吗?
    • 我也试过这个查询。如果没有 .name ,它会返回整个对象。 Schools.findOne({_id:Meteor.user().profile.schoolName});对象 {_id:“HGNzCYGszPGcG2Aab”,名称:“男生高中”}
    • 那么这一秒应该在最后使用 .name 。如果您保留在 profile.schoolName "HGNzCYGszPGcG2Aab" 中,则应将其与 id 进行比较,如果您有 "High School For Boys",则应将其与查询中的名称进行比较
    • 我已按照您的建议将代码更改为使用 _id 但不幸的是,将 .name 添加到最后会导致未定义的错误。感谢您的帮助。
    • 嗨,我在做木板。这是有效的,我只是将 .name 放在不正确的位置。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多