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!