0

一段时间以来,我一直在努力为特定用户显示用户配置文件,并且我取得了相当大的成功。我遇到标题错误的问题。

我发布了这样的学校收藏

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: Cannot read property 'name' of undefined 并且它来自 Schools 集合。有人可以帮我找出我的错误吗?

4

1 回答 1

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,如果你有改变的_idprofile.schoolNamename_id

于 2014-11-22T13:08:08.330 回答