【问题标题】:How to display property value linked by nested reference如何显示由嵌套引用链接的属性值
【发布时间】:2017-10-02 10:23:09
【问题描述】:

不确定我想要做的事情是否可行。

我想通过从引用用户架构(模型)的评论架构(模型)的文章文档中检索数据,在我的用户架构(模型)中显示“名称”属性的值。

'article' 引用了 'comment',它引用了 'user',其中包含我想要检索的 'user.name' 值。

通过 ID 数据检索文章并填充“作者”和“cmets”字段的控制器代码如下所示:

    Article.findById(req.params.id)
      .populate('author')
      .populate('comments')
      .exec(function(err, article){
         if(err){
            return req.flash('error', 'Unable to display article.');
         } else {
            res.render('articles/show', {
               article: article
            });
         }
     });

我的文章架构(模型)包含一个像这样的“cmets”属性:

  ... 
  comments: [
      {
         type: Schema.Types.ObjectId,
         ref: "Comment"
      }
   ],
   ...

我的评论架构(模型)包含一个“作者”属性,如下所示:

   ...
   text: String,
   author:
   {
      type: Schema.Types.ObjectId,
      ref: "User"   
   },
   ...

用户架构(模型)具有“名称”属性,该属性包含我想要在视图 (EJS) 中显示的值。

所以,到目前为止,我一直无法找到显示它的方法,可能是因为没有其他查询我无法做到。

我遍历 'article.cmets' 数组并成功显示带有 <%= comment.text %> 的评论文本,但我无法从评论中引用的 'user' 架构(模型)中找到显示 'name' 属性的方法架构(模型)。

对于我的错误的任何见解,我将不胜感激。

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    这是可能的。您正在尝试做的是深度人口。

    简单的做

    Article.findById(req.params.id).populate({
        path: 'comments',
        populate: {
            path: 'author'
        }
    }).exec(function(err, article){
        if (err)
            return req.flash('error', 'Unable to display article.');
        res.render('articles/show', { article: article });
    });
    

    在你看来,对于每条评论,你会做类似的事情

    <%- comment.author.name %>
    

    欲了解更多信息,请查看此link

    【讨论】:

    • 谢谢米奇。已实现并出现错误:“无法读取未定义的属性 'name'”我可以显示 comment.textcomment._id 但不能显示来自 'comment.author' 中引用的用户架构的属性
    【解决方案2】:

    对深层人口的额外研究并没有为我提供答案。这就是我所做的。

    我修改了我的评论架构并创建了“id”、“用户名”和“名称”属性/字段,如下所示:

    ....
    var Schema = mongoose.Schema;    
    var commentSchema = new Schema({
       text: String,
       author: {
          id:
          {
             type: Schema.Types.ObjectId,
             ref: "User"    
          },
          username: String, 
          name: String      
       },
       ....
    

    可能违反了 DRY,但它可以让我获得需要显示的数据。

    填充 cmets 和 author 字段的我的查询:

    Article.findById(req.params.id)
       .populate('comments')
       .populate('author')
       .exec(function(err, article){
          if(err){
             console.log(err);
             return req.flash('error', 'Unable to display article.');
          } else {
             res.render('articles/show', {
                article: article
             });
          }
       });
    

    我怀疑人口稠密是最好的答案,但时间有时会迫使我们继续前进。希望这可以帮助处于类似情况的人。感谢 Mikey 和 Ayush 的帮助。

    【讨论】:

      【解决方案3】:

      你必须做深人口

      Article.findById(req.params.id).populate({ path: 'article', populate:{ path: 'comments', populate: { path: 'user' } } }).exec(function(err, article){ if (err) return req.flash('error', 'Unable to display article.'); res.render('articles/show', { article: article }); });

      【讨论】:

      • 谢谢阿尤什。我收到同样的错误:“无法读取未定义的属性‘名称’”。 path 的值是模式中的属性还是模型名称?模型名称是文章、评论和用户。属性是“article.cmets”、“comment.author”和“user.name”。
      • @Jimv814 您必须输入要填充的字段名称。
      猜你喜欢
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      相关资源
      最近更新 更多