Hi All,
I have the following Model. Made up of Chats
that contain Messages
.
models.d.ts
declare module 'api/models' {
interface Chat {
_id?: string;
memberIds?: string[];
title?: string;
picture?: string;
lastMessage?: Message;
lastMessageCreatedAt?: Date;
receiverComp?: Tracker.Computation;
lastMessageComp?: Tracker.Computation;
}
interface Message {
_id?: string;
chatId?: string;
senderId?: string;
ownership?: string;
content?: string;
createdAt?: Date;
}
}
I am retrieving the Chats
as follows:
const chats: Mongo.Cursor<Chat> = Chats.find({
memberIds: this.senderId
}, {
transform: this.transformChat.bind(this),
// sort: {
// lastMessage: {createdAt: -1}
// },
fields: {
memberIds: 1
}
});
Question
How do I sort the Chats
result set by the createdAt
of its last Messages
?
More info
After the result set is obtained, the lastMessage
is set. This is done in the transformChat
function seen being invoked above (transform: this.transformChat.bind(this)
).
chat.lastMessage = this.findLastMessage(chat);
private findLastMessage(chat: Chat): Message {
return Messages.findOne({
chatId: chat._id
}, {
sort: { createdAt: -1 }
});
}
It’s after this I think, that I need to sort the Chats
result set.