1

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

sparkandy
  • 11
  • 3

1 Answers1

0

From the sound of it, you are looking for a multi-nested populate which is not supported in Waterline. You will get just the ID of the second nested record but nothing else, that is:

Routes = All Attributes
Sroute = All Attributes
Source = Only ID

This question has been asked before here and a workaround is given there.

If interested, you can also take a look at Waterline2 which does/will support nested populates but is not production ready, yet.

Community
  • 1
  • 1
Glen
  • 1,178
  • 10
  • 19