2014-11-22 20 views
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:无法读取未定义的属性'名称' 它是mese这是来自Schools集合。 有人可以帮我发现我的错误,请。

回答

1

首先,有更好的方法做你想做的achieve-包装所有的HTML与{{#with用户}}标签是这样的:

<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变化name_id

+0

喜有它_id,谢谢你的回答, 如你所说我已经修改了代码但我仍然未定义为错误。 我从浏览器的控制台运行查询,并得到这个回应 Schools.findOne((name:Meteor.user()。profile.schoolName.name}); undefined 您是否认为这可能是一个权限问题? – LanceHaig 2014-11-22 17:19:02

+0

我也试过这个查询。使用.name并返回整个对象。 Schools.findOne({_ id:Meteor.user()。profile.schoolName}); Object {_id:“HGNzCYGszPGcG2Aab”,name:“High School For Boys”} – LanceHaig 2014-11-22 17:25:49

+0

然后这一秒应该在最后使用.name。如果你保存在profile.schoolName“HGNzCYGszPGcG2Aab”中,那么你应该将它与ID相比较,如果你有“高中男生”,那么你应该把它与名称查询 – Sindis 2014-11-22 17:44:42

相关问题