首页 > 解决方案 > mongoose.populate() 未显示填充内容

问题描述

// 我的用户模式

const mongoose = require('mongoose');

常量 {ChatRoom} = 要求('./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

});

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

//聊天室模式

const mongoose = require('mongoose');

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

//消息模式

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

});

常量 ChatMsg = mongoose.models.ChatMsg || mongoose.model('ChatMsg', chatMsgSchema);

//聊天表模式

const chatTableSchema = new mongoose.Schema({

_id: mongoose.Schema.Types.ObjectId,
chats:[{type: mongoose.Schema.Types.ObjectId, ref: 'ChatMsg'}],

});

常量 ChatRoom = mongoose.models.ChatRoom || mongoose.model('ChatRoom', chatTableSchema);

module.exports = {ChatRoom, ChatMsg};

// 我的用于填充 chatList 的脚本

router.post('/room',ensureAuthenticated,异步函数(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 =>

既没有工作填充也没有过滤这些我适用于聊天列表

标签: javascriptmongoose-populate

解决方案


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

您可能需要调整样本。


推荐阅读