mongoose.populate未显示填充内容

问题描述

//我的用户架构

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,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 =用户;

// chatRoom模式

const mongoose = require('mongoose');

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

// msg模式

const chatMsgSchema = new mongoose.Schema({

_id: mongoose.Schema.Types.ObjectId,sender: { type: mongoose.Schema.Types.ObjectId,ref: 'User' },receiver: { type: mongoose.Schema.Types.ObjectId,msg: String,time: Number

});

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

// chatTable模式

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

//我的用于填充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);

});

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

既没有工作填充,也没有过滤我适用于chatList的内容

解决方法

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

尝试为您提供答案:您的代码有很多问题。

除非有更具体的问题,否则不要在架构中定义_id

更简洁的代码是:

// my user schema

import mongoose,{ Schema } from 'mongoose';

const userSchema = new Schema({
  username: {
    type: String,unique: true,},collegeEmail: {
    type: String,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,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,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',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);
});

您可能需要调整样本。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...