Meteor: The Correct way to access user information

133 views Asked by At

I'm new to meteor and I am in the process of building my application, but I am running into some "best practice" questions.

I have three databases. One for the users, one for questions, and one for answers. Think of my site as a quora of sorts.

I am iterating through the questions, and I am having a hard time accessing the user information. At the moment when a question gets recorded, I record the question, and I only record the user's id. Coming from a rails background, this is usually all I need to access all the other information when I need it. I don't record the name and all the other info.

The question I have is what is the right way to access the user information? I am doing an each loop and it seems that Meteor.user is only for logged in users (only for my information basically). I can't seem to do something like Meteor.users.find({_id: this.user_id}) on the questions helper so that I could get each users info. It retrieves nothing like this.

In the examples I have seen, they usually record the information on the questions database. So they would record first,last name, and anything else that would be necessary. But this seems like I am recording information twice.

What is the right way to access this info? Should I record it when a question is posted or should I call it from the users database everytime?

I have tried accessing the info through a server method, but can't seem to get it to work. Been at this for a while so I though I should ask instead.

Thanks in advance!!

2

There are 2 answers

0
Sasikanth On BEST ANSWER

My solution would be just save the user_id field in db as you're doing right now,

If you save firstname or lastname with the questions they may change in future (considering the fact that in most sites we have option to change names)

I can't seem to do something like Meteor.users.find({_id: this.user_id})

this is because user data is not availbale in the client, that is the reason why you can't see the data

solution is while publishing the question to client you should publish the information of the user too

Example

Meteor.publish('question,function(question_id){
  var question = Question.findOne(question_id);
  //get info of the question user
  var user = Meteor.users.find({_id: question.user_id });

//you cannot publish findone result, so do another query
  var ques_rec= Question.find({_id: question_id});

  return [ques_rec, user];
});

Now, in the client side when you do I can't seem to do something like Meteor.users.find({_id: this.user_id}) you'll get the user data.

0
socialight On

I think I found a solution. I haven't turned the autopublish off, so this seems to be working for now.

Template.questionItem.helpers({
  user: function(){
    user = Meteor.users.find({_id: this.user_id}).fetch();
    return user[0]
  }

I think basically you can search users, but for some reason if I do

 Meteor.users.find({_id: this.user_id}) 

it returns an object and I can't seem to find the actual data.

But if I add the .fetch() at the end it will return an array for the user. So if declare it as a variable, I can access all the info by saying user[0].

I'm not sure if if this is the best way to do it in meteor, but it does seem to work.

So when I call the object in the handlebars I go something like:

{{user.profile.name}}

I think once I turn autopublish off I will have to do the same thing but from the server side.