如何按日期分组和显示聊天消息

时间:2017-11-20 15:32:44

标签: node.js mongodb angular mongoose

我使用Nodejs,MongoDB(Mongoose)和Angular 5创建聊天应用程序。我想知道如何按日期拆分聊天消息。所以我可以显示哪些消息是在哪个日期发送的,就像whatsapp一样。

MongoDB - 架构。

let MessageSchema = new Schema({

        chat_id: {
            type: Schema.Types.ObjectId, ref: 'Chat',
            required: true,
        },
        body: {
            type: String,
            required: true,
        },
        author: {
            type: Schema.Types.ObjectId, ref: 'User',
            required: true,
        },
        isRead: Boolean,
    },
    {
        timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
    });

NodeJs - API函数/ chat /:id以获得整个对话

show(req, res) {

        let id = req.params.id;

        Chat.findOne({_id: id})
            .populate('participants')
            .populate({
                path: 'messages',
                populate: {path: 'author'},
            })
            .exec((err, chat) => {

                if (err) {
                    return res.status(404).json({

                        success: false,
                        status: 400,
                        data: err,
                        message: 'Error retrieving user conversations',
                    });
                }

                return res.status(200).json({

                    success: true,
                    status: 200,
                    data: chat,
                    message: 'Successfully retrieve conversation',

                });

            });

    }

Angular - chat.component.ts

   getConversation() {

        this.route.params.switchMap((params) => {
            let conversation_id = params['id'];
            return this.conversationService.get(conversation_id);
        }).subscribe((res) => {
            this.conversation = res.data;
            this.getMessages(this.conversation._id); // Socket call
        });
    }


<ng-template ngFor let-message [ngForOf]="conversation?.messages" let-i="index">

0 个答案:

没有答案