Getting an individuals email and addressinformation from Meteor.users

Hi All,

I’m trying to generate a report that lists the number of children signed up for camp.

In the html, I iterate through each camper and try to get a guardians email address by calling a function to retrieve the information from the Meteor.users collection.

<tbody *ngFor="let camper of campers; let i = index">
	<tr>
		<td>{{ i + 1}}</td>
		<td>{{ camper.lastName }}</td>
		<td>{{ camper.firstName }}</td>
		<td>{{ getGuardianEmail(camper.guardian) }}</td>
		<td>{{ camper.dob }}</td>
		<td>{{ camper.gender }}</td>
	</tr>
</tbody>

In the getGuardianEmail I do:

    getGuardianEmail(guardianId: string): string {
        console.log("@getGuardianEmail guardian - ", guardianId);
        var user = Meteor.users.findOne({"_id":"guardianId"});
        console.log("user - ", user);
        return user.emails[0].address;
    }

However the user does exist even though the guardian that is passed through is a valid _id.

From console:

@getGuardianEmail guardian -  CrFe8ktsAmKEga4Wp
user -  undefined 
Exception from Tracker recompute function: yada yada yada used by: Cannot read property 'emails' of undefined

I have a similar function that tries to get the address information.

Any help would be appreciated.

var user = Meteor.users.findOne({"_id":"guardianId"});

Remove the quotes around “guardianId” :wink:

1 Like

Thanks for the quick reply! You’re right the quotes should be removed, however I’m still getting the same results! :dizzy_face:

Have you subscribed to the guardians?

I have this in my publications/users.ts file:

import { Meteor } from 'meteor/meteor';

Meteor.publish("directory", function () {
    return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});

And in my report component’s constructor I have:

Meteor.subscribe("directory");

Hi All,

Just wondering if anyone out there may have some other ideas.
Is what I’m trying to do possible?
Has anyone accomplished something similar?

TIA

Remember that the guardian object won’t necessarily be available at the same time the campers are. So you might end up with a situation where the campers are loaded, they try to find the guardian, but the guardian sub has not synchronized yet, and then crashes.

In your return line, try
return user && user.emails[0].address;
It will not return anything until the user is available.

Also, having a pub like this Meteor.users.find({} .. with no selector or limit might be a bad idea if you have thousands of users, right? I would sub only to the guardians.

Hi @jorgeer,

Thanks for the reply. I’m not sure I follow why the guardians wouldn’t be loaded.
The guardian is a field within the campers collection and it shares a 1:1 relationship to a Meteor.user’s _id.
I also ensure the handle to the subscription is ready in the constructor.

Is the Meteor.users collection not ready? Perhaps I need to ensure that it’s ready as well?

Do you have 2 different collections, and 2 different subscriptions? Could you provide the constructor?