Show all Messages of One Conversation

Hello i am trying to build a chat live in a meteor App
well All it’s fine but i am having a little problem in showing the messages of one conversation in two seperated div “sent” and repley
this is a screenshot of my app

well in my helper i am getting the messages of a clicked conversation, i have a field called TypeMessage; if TypeMessage==“1” then i want to show that message in the sent div
and if TypeMessage==“2” then i want to show that message in the repley div
this is my helper code

 allMessagesOfConversation: function() {
        Meteor.subscribe('allConversations');
        Meteor.subscribe('allMessages');
        Meteor.subscribe('allContacts');

        return Messages.find({idConversation: Session.get('conversationId')},{sort: {createdAt: -1}}).map(function(message, messageSender) {
            if (message.typeMessage ==="1") {
                message.isFirst=true;
                return messageSender;
            }else {
                return message;

            }
        });

    },

and this is my template code

 <div class="messages">
                    {{#each allMessagesOfConversation}}
                        <ul>
                            {{#if isFirst}}

                                <li class="sent">
                                    <img src="http://emilcarlsson.se/assets/mikeross.png" alt="" />
                                    <p>{{message}} </p>
                                </li>
                            {{else}}

                                <li class="replies">
                                    <img src="http://emilcarlsson.se/assets/harveyspecter.png" alt="" />
                                    <p>{{message}}</p>
                                </li>
                            {{/if}}
                        </ul>
                    {{/each}}
                </div>

correct me please

if (message.typeMessage === "1") {
  message.isFirst = true;
  return messageSender;
} else {
  return message;
}

Looks like you’re returning messageSender when you should be returning message:

if (message.typeMessage === "1") {
  message.isFirst = true;
}
return message;
2 Likes

And you should to do your subscriptions in the template’s onCreated hook, not in the helper

Also the map function on a cursor won’t pass (message, messageSender) to the callback.
Cursor.map passes three arguments to the callback function: the document, a 0-based index, and cursor itself.

1 Like

thanks for replying , i changed my code and it worked

Thanks coagmano , i worked with your advice