Javascript 保存条目不需要';是否不将引用添加到我的其他文档?
我有一个ProfileSchema,我想跟踪已经发布的帖子。当我保存一篇新文章时,它有一个Profile_id,但是当我获取Profile条目时,posts数组是空的。这是怎么回事 配置文件模式Javascript 保存条目不需要';是否不将引用添加到我的其他文档?,javascript,mongoose,Javascript,Mongoose,我有一个ProfileSchema,我想跟踪已经发布的帖子。当我保存一篇新文章时,它有一个Profile_id,但是当我获取Profile条目时,posts数组是空的。这是怎么回事 配置文件模式 const mongoose = require('mongoose'); const Schema = mongoose.Schema; const ProfileSchema = new mongoose.Schema({ user: { type: Schema.Types.Obje
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProfileSchema = new mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
handle: {
type: String,
required: true,
max: 40
},
posts: [
{
type: Schema.Types.ObjectId,
ref: 'posts'
}
],date:{
type: Date,
default: Date.now
}
})
module.exports = Profile = mongoose.model('profile', ProfileSchema);
后模式
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
profile: {
type: Schema.Types.ObjectId,
ref: 'profile'
},
title: {
type: String,
required: true,
max: 40
},
body: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = Profile = mongoose.model('posts', PostSchema)
我如何保存它
router.post('/', passport.authenticate('jwt', {session: false}), (req,res) => {
const newPost = new Post({
title: req.body.title,
body: req.body.body,
img: req.body.img,
user: req.user._id,
profile: req.body.profile_id
});
newPost.save().then(post => res.json(post));
})
我有点困惑,mongoose文档使用的语法我不熟悉,或者还没有学会它们的方式。我的user
在我获取配置文件数据时填充,但填充时posts
数组为空