javascript - 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 =>
既没有工作填充也没有过滤这些我适用于聊天列表
解决方案
- 欢迎来到堆栈溢出
- 向 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);
});
您可能需要调整样本。
推荐阅读
- vhdl - VHDL 状态机正在跳过状态
- c++ - QFile::size() 和读取文件后部署的缓冲区大小不一样
- python - 如何使用 Python 在命令行中创建交互式应用程序,而不仅仅是返回输出的脚本?
- if-statement - 在 If 语句中舍入乘积
- wordpress - Facebook 没有找到 og:image 属性,但我在我的代码中查找了它,实际上它是用图像 url 明确声明的
- javascript - Spotify Web api 上的错误 400。需要帮助请
- python - 从任何位置对包裹的最佳参考
- java - How do I "obtain transaction-synchronized Session for current thread" in a Micronaut / Kotest / Hibernate test
- reactjs - 材质 UI 错误 发生意外错误:
- python - Pytest 引发不捕获停止迭代错误