I am working on a sails.js project and I have been having a challenge.
So, I have three models with one to many relationships
Source, Sroutes and Routes
Source has many Sroutes Sroutes has many routes
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',
},
}
};
This is my problem. I want to be able to do a for loop for routes
<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>
The above code show "undefined" when I try to pull the name of the source
Below is my index function in RouteController
index: function(req, res, next) {
Route.find().populateAll().exec(function findRoute(err, routes) {
if (err) return next(err);
res.view({
routes: routes
});
});
},
Thanks