[英]How to populate a mongoose schema
I have the following mongoose schemas 我有以下猫鼬模式
var postTable = mongoose.Schema({
userPost:{type : String},
dateCreated: {type: Date, default: Date.now},
_replies:[{type: mongoose.Schema.Types.ObjectId, ref: 'reply_table'}],
_creator:{type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});
and 和
var reply_table = mongoose.Schema({
userReply:{type:String},
date_created:{type:Date, default: Date.now},
_post:{type: mongoose.Schema.Types.ObjectId, ref: 'post'},
_creator:{type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});
var userPost = module.exports = mongoose.model("Post",postTable);
var userReply = module.exports = mongoose.model('reply_table',reply_table);
User can create post which will be entered into the Post table and other users can comment or reply to a post which will be entered into the reply_table. 用户可以创建将被输入到Post表中的帖子,其他用户可以评论或回复将被输入到Reply_table中的帖子。 I then try to populate the the post table like this 然后我尝试像这样填充post表
module.exports.getPost = function (callback) {
var mysort = { dateCreated: -1 };
userPost
.find({},callback)
.sort(mysort)
.populate('_creator','username')
.populate(' _replies')
.exec(function (err,post) {
if(err) throw err;
console.log(post)
});
};
When the console prints out the post it prints the post information and a object with the user information becausei have another schema setup for users, therefore I used .populate('_creator','username')
当控制台打印出帖子时,它会打印帖子信息和带有用户信息的对象,因为我为用户提供了另一种模式设置,因此我使用了.populate('_creator','username')
The problem is it wont print the reply information it only prints an empty array: reply[]. 问题在于它不会打印答复信息,而只会打印一个空数组:reply []。
I'm pretty sure I'm doing everything right. 我很确定自己做的一切正确。 I used the following code to insert information into the reply_table 我使用以下代码将信息插入reply_table
//make a reply on a post
module.exports.make_reply = function (user_id,pid,reply,callback) {
var newReply = userReply({
_creator: user_id,
_post: pid,
userReply: reply
});
newReply.save(callback);
}
I know this question is very long but does anyone have any idea of what I might be doing wrong. 我知道这个问题很长,但是没有人知道我可能做错了什么。 I only want to populate the Post schema with information from the reply_table 我只想使用来自reply_table的信息来填充Post模式
I finally figured out a solution to my question. 我终于想出了解决我问题的方法。 What i did was i created a function to insert the reply id into the post table. 我所做的是创建了一个将回复ID插入帖子表的函数。 It basically get the comment by its id and push a reply into the _replies array in the post table. 它基本上通过其ID获取评论,并将回复推入post表的_replies数组中。
//Insert reply into post table
module.exports.addReply = function (id,reply) {
userPost.update({_id:id},{$push:{replies:reply}},{multi:true},function
(err,post) {
});
}
When i use the getPost function it populates the reply table 当我使用getPost函数时,它将填充回复表
module.exports.getPost = function (callback) {
var mysort = {dateCreated: -1};
userPost
.find({}, callback)
.sort(mysort)
.populate('_creator', 'username')
.populate('replies')
.exec(function (err) {
if(err) throw err;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.