Sails.js填充嵌套关联

Sails.js填充嵌套关联

我自己有一个关于Sails.js版本0.10-rc5中的关联的问题。我一直在构建一个应用程序,其中多个模型相互关联,我到达了一个我需要以某种方式获得嵌套关联的点。

有三个部分:

首先是博客文章,这是由用户编写的。在博客文章中,我想显示关联用户的信息,如用户名。现在,这里一切正常。直到下一步:我正在尝试显示与帖子相关的评论。

注释是一个单独的模型,称为Comment。每个人还有一个与之相关的作者(用户)。我可以轻松地显示评论列表,但是当我想显示与评论相关的用户信息时,我无法弄清楚如何使用用户的信息填充评论。

在我的控制器中,我试图做这样的事情:

Post
  .findOne(req.param('id'))
  .populate('user')
  .populate('comments') // I want to populate this comment with .populate('user') or something
  .exec(function(err, post) {
    // Handle errors & render view etc.
  });

在我的Post''show'动作中,我试图检索这样的信息(简化):

<ul> 
  <%- _.each(post.comments, function(comment) { %>
    <li>
      <%= comment.user.name %>
      <%= comment.description %>
    </li>
  <% }); %></ul>

但是,comment.user.name将是未定义的。如果我尝试只访问'user'属性,例如comment.user,它会显示它的ID。这告诉我,当我将评论与其他模型关联时,它不会自动将用户的信息填充到评论中。

任何理想的人都能妥善解决这个问题:)?

提前致谢!

PS

为了澄清,这就是我基本上在不同模型中建立关联的方式:

// User.jsposts: {
  collection: 'post'},   hours: {
  collection: 'hour'},comments: {
  collection: 'comment'}// Post.jsuser: {
  model: 'user'},comments: {
  collection: 'comment',
  via: 'post'}// Comment.jsuser: {
  model: 'user'},post: {
  model: 'post'}


眼眸繁星
浏览 654回答 3
3回答

慕尼黑的夜晚无繁华

或者您可以使用内置的Blue Bird&nbsp;Promise功能来制作它。(致力于Sails@v0.10.5)请参阅以下代码:var&nbsp;_&nbsp;=&nbsp;require('lodash');...Post &nbsp;&nbsp;.findOne(req.param('id')) &nbsp;&nbsp;.populate('user') &nbsp;&nbsp;.populate('comments') &nbsp;&nbsp;.then(function(post)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;commentUsers&nbsp;=&nbsp;User.find({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;_.pluck(post.comments,&nbsp;'user') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//_.pluck:&nbsp;Retrieves&nbsp;the&nbsp;value&nbsp;of&nbsp;a&nbsp;'user'&nbsp;property&nbsp;from&nbsp;all&nbsp;elements&nbsp;in&nbsp;the&nbsp;post.comments&nbsp;collection. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(function(commentUsers)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;commentUsers; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[post,&nbsp;commentUsers]; &nbsp;&nbsp;}) &nbsp;&nbsp;.spread(function(post,&nbsp;commentUsers)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;commentUsers&nbsp;=&nbsp;_.indexBy(commentUsers,&nbsp;'id'); &nbsp;&nbsp;&nbsp;&nbsp;//_.indexBy:&nbsp;Creates&nbsp;an&nbsp;object&nbsp;composed&nbsp;of&nbsp;keys&nbsp;generated&nbsp;from&nbsp;the&nbsp;results&nbsp;of&nbsp;running&nbsp;each&nbsp;element&nbsp;of&nbsp;the&nbsp;collection&nbsp;through&nbsp;the&nbsp;given&nbsp;callback.&nbsp;The&nbsp;corresponding&nbsp;value&nbsp;of&nbsp;each&nbsp;key&nbsp;is&nbsp;the&nbsp;last&nbsp;element&nbsp;responsible&nbsp;for&nbsp;generating&nbsp;the&nbsp;key &nbsp;&nbsp;&nbsp;&nbsp;post.comments&nbsp;=&nbsp;_.map(post.comments,&nbsp;function(comment)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment.user&nbsp;=&nbsp;commentUsers[comment.user]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;comment; &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;res.json(post); &nbsp;&nbsp;}) &nbsp;&nbsp;.catch(function(err)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;res.serverError(err); &nbsp;&nbsp;});一些解释:我正在使用Lo-Dash来处理数组。有关详细信息,请参阅官方文档注意第一个“then”函数内的返回值,数组中的那些对象“[post,commentUsers]”也是“promise”对象。这意味着它们在首次执行时不包含值数据,直到它们获得值。因此,“传播”功能将等待动作值来继续做剩下的事情。
打开App,查看更多内容
随时随地看视频慕课网APP