0
votes

I'm trying to return the profile.name from a meteor user from an ID passed in from a template.

I've made a template helper that calls a meteor method (as I know to get to users other than the logged in one I need to be on the server) and have confirmed that the user ID is being passed correctly, but it blows up with the find query (or findOne) returning undefined.

The method looks like

hostUser: function(userID) {
    //var id = _.pick(attribs, 'userID');
    console.log(userID);
    var user = Meteor.users.find({_id: userID});
    console.log(user);
    return user.profile.name;
  }

which is within Meteor.methods in the /lib directory

The console.log(userID) call is correct, but the find (or findOne) returns null.

db.users.findOne({_id:"sAhoXRtwqkDyMwK9"}) returs the correct user (if run from the mongo console).

1
What happens if you manually call the method from the console? e.g., replace find with findOne and do: Meteor.call('hostUser', 'sAhoXRtwqkDyMwK9', function(err, name){console.log(err, name);}); - David Weldon
I moved the method into server (which is where it should have been anyway as pointed out below) and changed it to findOne. I changed it to just console.log(user); return "Fred"; to stop it blowing up but user is undefined. It has to be something simple surely. Just for testing I changed it back to find, in the console I can see _cursorDescription: { collectionName: 'users', selector: {_id: "sAhoXRTwqkDyMwK9" }, options: {transform: null } } - Peter Nunn
I think I was looking at the wrong thing (err rather than name). Its now working having moved it to server, but, still not quite. The template helper is getting the right value, but its not displaying in the template itself. I'll look for this error and post another question if I can't work that out. - Peter Nunn
Try placing the helper's function body within a Deps.autorun block. - Serkan Durusoy
I think I'm getting in over my head here!! Is this what you had in mind? Template.projectPage.helpers ({ posterUserName: function() { Deps.autorun(function() { console.log(this.userID); Meteor.call('hostUser', this.userID, function (error, result) { if(error) { throwError(error.reason); } else { console.log("Result:"+result.profile.name); return result.profile.name; } }); }) } }) Formatting is all over the place.. but the first console.log is never displayed and undefined returned - Peter Nunn

1 Answers

2
votes

The lib directory is still available to both client and the server.

If you want the method to be server only, either place it somewhere within the server directory, or surround it with block like:

if (Meteor.isServer) {
 // method definitions
}