【问题标题】:Displaying list in view for one to many relationship sails.js waterline orm在视图中显示一对多关系sails.js waterline orm的列表
【发布时间】:2017-05-10 20:09:24
【问题描述】:

我正在从事一个sails.js 项目,我一直面临着挑战。

所以,我有三个具有一对多关系的模型

来源、路线和路线

Source 有很多 Sroutes Sroutes有很多路线

Source.js
module.exports = {

attributes: {

    name: {
        'type': 'string',
        'required': true,
    },

    shortname: {
        'type': 'string',
        //'required': true,
    },

    sroute: {
        collection: 'sroute',
        via: 'source',
    },


  }
};


Sroute.js
module.exports = {

   attributes: {

    source: {
        'type': 'integer',
        'required': true,
        model: 'source',
    },

    destination: {
        'type': 'integer',
        'required': true,
        model: 'destination',
    },

    code: {
        'type': 'string',
    },

    routes: {
        collection: 'route',
        via: 'sroute'
    }
   }
};


Routes
module.exports = {

   attributes: {

    sroute: {
        'type': 'integer',
        'required': true,
        model: 'sroute',
    },

    cost: {
        'type': 'float',
    },

   }
};

这是我的问题。我希望能够为路由做一个 for 循环

<table>
 <tr>
    <th>Route ID</th>
    <th>Source</th>
    <th>Cost</th>

</tr>
   <% _.each(routes, function(route) {%>
    <tr data-id="<%= route.id %>" data-model="route">
        <td>
            <%= route.id %>
        </td>
        <td>
            <%= route.sroute.source.name %>
        </td>
             <td>
            <%= route.cost %>
        </td>
    </tr>
    <% }) %>
</table>

当我尝试拉取源名称时,上面的代码显示“未定义”

下面是我在RouteController中的索引函数

index: function(req, res, next) {
 Route.find().populateAll().exec(function findRoute(err, routes) {

   if (err) return next(err);

     res.view({
      routes: routes
     });
 });
},

谢谢

【问题讨论】:

  • route.sroute 存在还是未定义?
  • @chasenyc 是的。 route.sroute 存在。
  • 还有 route.sroute.source?
  • @chasenyc 是的,它有效。当我添加 .name 时,它​​显示“未定义”

标签: node.js orm sails.js waterline


【解决方案1】:

从它的声音来看,您正在寻找 Waterline 不支持的多嵌套填充。您将只获得第二个嵌套记录的 ID,而不会获得其他任何信息,即:

路线 = 所有属性
Sroute = 所有属性
来源 = 只有 ID

here 之前已经提出过这个问题,并且那里给出了解决方法。

如果有兴趣,您还可以查看Waterline2,它确实/将支持嵌套填充但尚未准备好生产。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多