提问者:小点点

Mongoose.populate()未显示populate内容


//我的用户架构

const mongoose=required('mongoose');

const{ChatRoom}=require(“。/ChatRoom”);

常量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

});

常量User=mongoose.models.User mongoose.model('User',userSchema);module.exports=用户;

//聊天室架构

const mongoose=required('mongoose');

常量User=require('./User');

//消息架构

常量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);

//可支付架构

常量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};

//我的填充聊天列表脚本

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);

});

在控制台上显示所有chatList=>;

既没有工作填充,也没有过滤这些我应用到聊天列表


共1个答案

匿名用户

  1. 欢迎使用堆栈溢出
  2. 向SO提交问题时,请设置问题的格式,以便代码和消息之间有明显的区别。

尝试并为您提供一个答案:您的代码有许多问题。

您不应该在架构中定义_id,除非您有更具体的问题。

更简洁的代码是:

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

您填充数据的方式有点不正常:

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);
});

您可能需要调整样品。