-1

// my 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;

//chatRoom schema

const mongoose = require('mongoose');

const User = require('./user');

//msg 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};

// my script for populate 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 =>

There is neither work populate and nor filter these I apply to chatList

RK NANDA
  • 71
  • 8

1 Answers1

1
  1. Welcome to Stack Overflow
  2. When submitting a question to SO, please format your question so there is an apparent distinction between code and message.

To try and provide you an answer : there are many problems with your code.

You should not define _id in your schemas, unless you have a more specific problematic.

A more cleaner code would be :

// my user schema

import mongoose, { Schema } from 'mongoose';

const userSchema = new Schema({
  username: {
    type: String,
    unique: true,
  },
  collegeEmail: {
    type: String,
    unique: true,
  },
  password: { type: String },
  photo: { type: String },
  name: { type: String },
  phoneNo: { type: Number },
  collegeName: { type: String },
  gender: { type: String },
  follow: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
  following: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
  chatList: [{userId:{type: mongoose.Schema.Types.ObjectId, ref: 'User'}, chatId:{type: mongoose.Schema.Types.ObjectId, ref: 'ChatRoom'}}],
  bio: { type: String },
  lastSeen: { type: Number },
  active: { type: Boolean },
  status: { type: Boolean },
  otp: { type: Number },
});

const User = mongoose.model('User', userSchema);
module.exports = User;

//msg schema

const chatMsgSchema = new Schema({
  sender: { type: Schema.Types.ObjectId, ref: 'User' },
  receiver: { type: Schema.Types.ObjectId, ref: 'User' },
  msg: { type: String },
  time: { type: Number },
});

const ChatMsg = mongoose.model('ChatMsg', chatMsgSchema);

//chatTable schema

const chatTableSchema = new Schema({
  chats: [{ type: Schema.Types.ObjectId, ref: 'ChatMsg' }],
});

const ChatRoom = mongoose.model('ChatRoom', chatTableSchema);

module.exports = { ChatRoom, ChatMsg };

// my script for populate 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('chatList')
    .exec();
  console.log(frnd);
});

EDIT

The way you're populating your data is a bit off It should be :

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.chatId', 
       model : 'ChatRoom'
    })
    .populate({
       path : 'chatList.userId', 
       model : 'User'
    })
    .exec();
  console.log(frnd);
});

You may need to adjust the samples.

CrazyYoshi
  • 1,003
  • 1
  • 8
  • 19
  • Bro actually i need userId in chatList, then how can i store it. – RK NANDA Nov 05 '20 at 10:17
  • 1
    First, userid isn't equal to `this`. Second, this can't be set at schema definition. Second : Since you're getting your chatlist from the user, you already have your UserId. I'll edit my answer to provide you a simple example. – CrazyYoshi Nov 05 '20 at 10:34
  • this => type : mongoose.Schema..Type.ObjectId. – RK NANDA Nov 05 '20 at 16:23
  • like follow and following array i used this keyword, It is working .And in userId, I am not storing my own id. In this, the id of the user with whom I want to chat will be protected. – RK NANDA Nov 05 '20 at 16:31
  • Okay I understand a little bit better your thinking now. I never used the this keyword at schema definition, I'm used to referencing using model and object id. I'll edit my answer to strictly answer to your problematic. – CrazyYoshi Nov 05 '20 at 19:23
  • I check your whole solution there are you change this keyword to by the given type of object. but when u replace this keyword to ref own type user model then nodejs occur a problem u can't use before the definition . it not error of mongoose, so that i user this keyword. and other thinh here that chatfilter not work if i go with u by use virtul . but it is not use full without populate (populate is not working ) – RK NANDA Nov 06 '20 at 08:08
  • I didn't understand everything you said, but for your original question, you just have to take the part that interest you which is below the EDIT title.Please just try to change this part of your code. – CrazyYoshi Nov 06 '20 at 08:10