Lance Haig
Lance Haig

Reputation: 332

Exception in template helper: Cannot read property 'name' of undefined

I have been struggling with getting a userprofile to display for a specific user for some time now and I have been reasonably successful. I have ht oe problem where I get the error in the heading.

I publish a schools collection like this

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

My subscription looks like this

Meteor.subscribe('schoolData');

My HTML looks like this

<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>

and the javascript looks like this

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},
});

I seem to get the Error Exception in template helper: TypeError: Cannot read property 'name' of undefined and it mees this is from the Schools collection. Can someone help me spot my mistake please.

Upvotes: 0

Views: 2793

Answers (1)

sdooo
sdooo

Reputation: 1881

First of all, there is better way to do what you want to achieve- wrap all your html with {{#with user}} tags like this:

<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>

And helper like:

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

Now you can use all data from your user collection in html without helpers

And to your question - you made your query wrong, it should look like this:

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

I assumed you want to check school by name, if you have _id of it in profile.schoolName change name with _id

Upvotes: 1

Related Questions