3

In sails.js I have the following models:

Site

module.exports = {
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    active: {
        type: 'boolean',
        defaultTo: false
    },
    pages: {
        collection: 'page',
        via 'site'
    }
  }
};

Page

module.exports = {
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    site: {
        model: 'site',
        required: true
    },
    modules: {
        collection: 'module',
        via 'page'
    }
  }
};

Module

module.exports = {
  attributes: {
    module: {
        type: 'string',
        required: true
    },
    page: {
        model: 'page',
        required: true
    }
  }
};

When I call GET /Site/1 I get the following:

{
    "pages": [
        {
            "name": "First page",
            "site": 1,
            "createdAt": "2014-08-23T17:57:41.562Z",
            "updatedAt": "2014-08-23T17:57:41.562Z",
            "id": 1
        }
    ],
    "name": "First site",
    "createdAt": "2014-08-23T17:56:57.143Z",
    "updatedAt": "2014-08-23T17:56:57.143Z",
    "id": 1
}

I'm using MongoDB and this would be dead easy to model as a nested document.. unfortunately I don't think Waterline supports that, hence the assocations/joins.

I can see that it's successfully outputting each page associated with a site, how do I make it so it also outputs the list of modules that are associated with each page?

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
richwol
  • 1,065
  • 2
  • 11
  • 24
  • possible duplicate of [Sails.js populate nested associations](http://stackoverflow.com/questions/23446484/sails-js-populate-nested-associations) – sgress454 Aug 26 '14 at 05:10

1 Answers1

3

This thread might help. Essentially, there is no direct way to grab nested associations yet.

Community
  • 1
  • 1
kk415kk
  • 1,227
  • 1
  • 14
  • 30