模板助手中的例外:无法读取属性' name'未定义的

时间:2014-11-22 12:43:32

标签: meteor meteor-accounts

我一直在努力让用户配置文件为特定用户显示一段时间,而且我已经取得了相当的成功。我有问题,我在标题中得到错误。

我发布了这样的学校集合

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:无法读取属性&#39; name&#39;未定义的 它来自学校系列。 有人可以帮我发现我的错误。

1 个答案:

答案 0 :(得分:1)

首先,有更好的方法来实现您想要实现的目标 - 使用{{#with用户}}标签包装所有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()});

现在,您可以在没有帮助程序的情况下使用来自用户集合的所有数据

对于你的问题 - 你的查询错了,它应该是这样的:

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

我认为您希望name检查学校,如果您_id profile.schoolName name _id {{1}} {{1}}