【发布时间】: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