// defining user schema
const mongoose = require('mongoose');
const {ChatRoom} = require('./chatRoom');
const userSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
username:{
type: 'String',
unique: true,
},
collegeEmail:{
type: String,
unique: true,
},
password: String,
photo: String,
name: String,
phoneNo: Number,
collegeName: String,
gender: String,
chatList:[{userId:this, chatId:{type: mongoose.Schema.Types.ObjectId, ref: 'ChatRoom'}}],
bio: String,
follow:[ this],
following:[ this],
lastSeen: Number,
active: Boolean,
status: Boolean,
otp: Number
});
const User = mongoose.models.User || mongoose.model('User', userSchema); module.exports = User;
//defining chatRoom schema
const mongoose = require('mongoose');
const User = require('./user');
//message schema
const chatMsgSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
sender: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
receiver: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
msg: String,
time: Number
});
const ChatMsg = mongoose.models.ChatMsg || mongoose.model('ChatMsg', chatMsgSchema);
//chatTable schema
const chatTableSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
chats:[{type: mongoose.Schema.Types.ObjectId, ref: 'ChatMsg'}],
});
const ChatRoom = mongoose.models.ChatRoom || mongoose.model('ChatRoom', chatTableSchema);
module.exports = {ChatRoom, ChatMsg};
// script for populating chatList
router.post('/room',ensureAuthenticated,async function(req, res) {
const id = req.body.id;
console.log(id);
const frnd = await User.
findOne({username: req.user.username}).
populate({
path: 'chatList',
model: 'ChatRoom',
match: { _id: id}
}).
exec();
console.log(frnd);
});
on the console show all chatList =>
No output from the populate and filter on the chatList was observed.