在视图中显示一对多关系sails.js waterline orm的列表

时间:2017-05-10 20:09:24

标签: node.js orm sails.js waterline

我正在开发一个sails.js项目,我一直在接受挑战。

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

来源,路线和路线

来源有很多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
     });
 });
},

由于

1 个答案:

答案 0 :(得分:0)

从它的声音来看,您正在寻找Waterline不支持的多嵌套填充。您将只获得第二个嵌套记录的ID,但没有其他内容,即:

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

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

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