node.js - Mongoose 显示每个帖子的评论和星星(喜欢)
问题描述
我一直在制作一个社交网络应用程序。用户可以发布帖子,其他用户可以对帖子加注星标(点赞)或评论。
每个帖子都包含一系列评论和星号。每个评论和星标都包含对其帖子的引用。
我的帖子、评论和明星的模块结构是这样的
星型图
const StarSchema = mongoose.Schema({
created_at : {
type : Date,
require : true
},
user : {
type : mongoose.Schema.Types.ObjectId,
ref : 'User'
},
post : {
type : mongoose.Schema.Types.ObjectId,
ref : 'Post'
}});
评论模式
const CommentSchema = mongoose.Schema({
post : {
type : mongoose.Schema.Types.ObjectId,
ref : 'Post'
},
user : {
type : mongoose.Schema.Types.ObjectId,
ref : 'User'
},
content : {
type : String ,
require : true
},
created_at : {
type : Date,
require : true
}});
后架构
const PostSchema = mongoose.Schema({
content : {
type : String,
require : true
},
created_at : {
type : Date,
require : true
},
user : {
type : mongoose.Schema.Types.ObjectId,
ref : 'User'
},
word : {
type : mongoose.Schema.Types.ObjectId,
ref : 'Word'
},
comments : [{
type : mongoose.Schema.Types.ObjectId,
ref : 'Comment'
}],
stars : [{
type : mongoose.Schema.Types.ObjectId,
ref : 'Star'
}]});
当我想使用此查询获取带有评论和星标的帖子时
Post.find({_id : postId}).populate('user').exec(callback);
它给我带来了帖子,但它的评论和星星是空的,即使它有评论和星星
解决方案
推荐阅读
- javascript - 改变表面图vis.js的颜色?
- spring-mvc - Spring Boot MVC -> Excel 数据在下载时损坏
- angular - 我正在使用 ngClass 更改按钮单击的颜色和文本,并使用数据表从远程服务器获取数据
- sql-server - 数据库备份到 Azure Blob
- c - 在C中按字典顺序排序
- neural-network - 为什么 pytorch 有两种非线性激活?
- r - R中的while循环内多元素向量与多元素向量比较的替代方法是什么?
- python - 在 Python 中将数据帧值插入 Azure 表存储时出现问题
- android - 在完美运行之前的 CMake 错误现在充满了错误?
- hadoop - 调试hadoop文件要求