In my Mongo collection documents, I have an array containing various objects for a chat app. When I try to display the data, I encounter an issue where all messages are getting the "my-message" class instead of just my messages.
I'm struggling to target only my messages with the my-message
class. How do I fix this?
Document Structure:
{
_id: "RCe9ZBS9PfkwrqKvg"
borrowerId: "Rg33iLJyRYqHyMpk4"
messages: Array[{text:messageBody, from:currentUserId, date:New Date()}]
ownerId: "fPv5yWJiSqFX3bAhR"
reqId: "KXErmpFJ7LiCbKsqc"
}
Template:
{{#with getMessages}}
{{#each messages}}
<p class="{{#if myMessages}} my-message {{/if}}">{{text}}</p>
{{/each}}
{{/with}}
Helper:
Template.reqMessages.helpers({
getMessages: function () {
var theReqId = FlowRouter.getParam("reqId");
return Messages.findOne({reqId: theReqId});
},
myMessages: function () {
var currentUserId = Meteor.userId();
var theReqId = FlowRouter.getParam("reqId");
return Messages.find({reqId: theReqId,
messages: [{$elemMatch: {
from: currentUserId
}}]});
}
});