Finding another user inside a method

I am working on adding messages to my application and I am having a little trouble trying to look up the other user(the one the message is going to) to grab the userId. I’ll explain how I am setting up messaging so you have an idea of what I am trying to achieve.

This is the model I am going for.

Convo:
_id:
userIds: [ ] // user ids for all who are in the convo. Only 2 people for now, but later will allow messaging between groups
// of users
lms: // last message sent date, for display purpose only

Messages:
_id:
convoId: // associates messages to a convo between users
userId: // userId of the user who created the message
username: // username of the user who created the message
createdAt: // date created
read: // if the message has been read

  • On the message page, I will list links to all convos where the current userId is in the array of userIds in the convo, listing them descending order by lms.
  • clicking on a convo will bring up all the messages associated with the convo, sorting descending order by createdAt.

I have a form that people type who they want to send the message to and what they want to say. my create convoMethod checks to see if an existing convo between users exist, if true, add the message to the existing convo, if not, create a new convo and add the message to the convo. My issue that I am having is in my create convo. I can not seem to find a way to look up the recipient user to add that userId to the array of userIds a new convo.

the only way that am able to return the user object is if I set the value in the returning result of the method like this:

Meteor.methods({
‘createConvo’: function() {
// code goes here
// VAR is just a representation for where the username from the new message form would go.
// I run all the checks to make sure the right data is being submitted

// this does not work
var recipient = function() {
  return Meteor.users.findOne({username: VAR});
}

// and this doesnt work either
var recipient = Meteor.users.findOne({username: VAR});

// this works
return {
  user: Meteor.users.findOne({username: VAR});
}

}
)};

My problem is I need to find the recipients ID prior to returning the result of the method. I even tried to call a method inside of me createConvo method but I was not able to get that to work either.

any help or advice would be appreciated. thanks!

I also have tried subscribing to a singleUser publication inside my method and then running the findOne method to see if I could get the user that way and had no such luck

Have you considered a ready made solution? https://atmospherejs.com/socialize/messaging

I have looked at a few including the one you linked. I am all for using ready made solutions, but for this app I’m just doing it to get a deeper understanding of how things work and how to make my own solutions as opposed to just grabbing a ready made. Thanks for the link though!

Just in case anyone else has the same question as m, I thought I would post the only solution that I could find on my own.

CLIENT
// inside of a template events

var findUser = {
 username: documentById('someId').value
}

Meteor.call('findUserId' findUser.username, function(error, result) {
   var message = _.extend(findUser,
      userId: result._id
   });
 Meteor.call('newMessage', message, function(error, result) {
       // do something will callback if needed.....
 });
});

SERVER

Meteor.methods({
  'findUserId': function(username) {
     return Meteor.users.findOne({username: username});
},

  'newMessage': function(message) {
    // code for creating a new message and inserting into mongo db
  }
});

This was the only way that I could get the userId of the username the current logged in user wanted to send the message to. I tried creating a function that you pass the username to and it returns the userId but you have to have a subscription to all the users on the client for that to work and I dont want all that info on everyones client while creating a new message. For some reason Meteor.users.find or even findOne does not actually return and store the object inside a variable in a meteor method. The only way to return the object is to return it as the method result which is odd. Hope this helps anyone who has or had the same problems as me trying to get a userId from a username.