鍍金池/ 問答/數(shù)據(jù)庫  HTML/ nodejs項(xiàng)目評(píng)論功能的實(shí)現(xiàn)

nodejs項(xiàng)目評(píng)論功能的實(shí)現(xiàn)

嘗試在用express+mongoose搗騰一個(gè)論壇, 實(shí)現(xiàn)評(píng)論功能的時(shí)候遇到一個(gè)問題。

帖子的model:

const ArticleSchema = new Schema({
  author: { type: Schema.Types.ObjectId, ref: "User" },
  createTime: { type: Date },
  thumb: [{ type: Schema.Types.ObjectId, ref: "User" }],
  comment: [{ type: Schema.Types.ObjectId, ref: "Comment" }],
  text: String
});

評(píng)論的model:

const CommentSchema = new Schema({
  text: String,
  createTime: { type: Date },
  poster: { type: Schema.Types.ObjectId, ref: 'User' },
  posts: {type: Schema.Types.ObjectId, ref: 'Article'}
})

渲染數(shù)據(jù):

  Article.find()
    .sort('-createTime')
    .populate('author')
    .populate('comment')
    .exec((err, articles) => {
      if (err) return next(err)
      res.render('container/index', {
        title: 'xxxx',
        articles
      })
    })

前端:

評(píng)論內(nèi)容:comments[i].text
評(píng)論人: comments[i].poster

能夠獲取到評(píng)論內(nèi)容, 但是評(píng)論者顯示的是_id值, 這樣的情況 難道我要再查詢一次User嗎?

回答
編輯回答
櫻花霓

deep-populate

User.
  findOne({ name: 'Val' }).
  populate({
    path: 'friends',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'friends' }
  });
2017年6月6日 00:09
編輯回答
妖妖

用聚合查詢.aggregate,或者你再查一次。。。

2018年7月30日 23:25