我正在研究sails.js项目,但遇到了挑战。

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

来源,路线和路线

源有许多路线
路线有很多路线

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>


当我尝试提取源名称时,上面的代码显示“ undefined”

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

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

   if (err) return next(err);

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


谢谢

最佳答案

从声音上看,您正在寻找Waterline不支持的多嵌套人口。您将只获得第二个嵌套记录的ID,而没有别的,即:

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

here之前已问过此问题,并在此处给出了解决方法。

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

07-24 09:31