0

I'm using SailsJS for the first time. I have set up some models with associations, and everything's working fine. Almost.

Here's my Project model:

module.exports = {
    attributes: {
        title: { type: 'string', required: true },
        subtitle: { type: 'string', required: false },
        image: { model: 'image' },
        bgColor: { type: 'string', required: false },
        tabPos: { type: 'integer', required: true, defaultsTo: 0 },
        featured: { type: 'boolean', required: true, defaultsTo: false },
        contentBlocks: { collection: 'contentBlock', via: 'project' },
        position: { type: 'integer', required: true }
    }
};

And here's my ContentBlock model:

module.exports = {
    attributes: {
        project: { model: 'project' },
        copy: { type: 'string', required: true },
        icon: { type: 'integer', required: false },
        images: { collection: 'image' },
        bgColor: { type: 'string', required: false },
        position: { type: 'integer', required: true }
    }
};

And finally, my Image model:

module.exports = {
    attributes: {
        filename: { type: 'string', required: true },
        alt: { type: 'string', required: true },
        device: { type: 'string', required: true, defaultsTo: 'laptop' },
        deviceColor: { type: 'string', required: false }
    }
};

All the associations are working great, except that the project endpoint doesn't include the contentBlock images in the payload. It doesn't even include an array of ids. It just ignores the attribute altogether:

[
    {
        contentBlocks: [
            {
                project: "5406ffb56d109ad149a14362",
                copy: "Lorum ipsum",

                // expecting array of image ids here
                // but its completely missing

                position: 0,
                createdAt: "2014-09-03T11:49:08.162Z",
                updatedAt: "2014-09-03T12:13:55.279Z",
                id: "540700346d109ad149a14363"
            }
        ],
        image: {
            filename: "test1_320x480.jpg",
            alt: "A test image",
            device: "laptop",
            createdAt: "2014-09-03T11:36:13.608Z",
            updatedAt: "2014-09-03T11:36:13.608Z",
            id: "5406fd2d6d109ad149a1435e"
        },
        title: "Project Alpha",
        subtitle: "A test project",
        position: 0,
        tabPos: 0,
        featured: false,
        createdAt: "2014-09-03T11:47:01.155Z",
        updatedAt: "2014-09-03T12:13:35.972Z",
        id: "5406ffb56d109ad149a14362"
    }
]

The contentBlock endpoint does include the images however.

[
    {
        images: [
            {
                filename: "test2_320x480.jpg",
                alt: "Another test image",
                device: "ipad",
                deviceColor: "#FFF",
                createdAt: "2014-09-03T11:36:50.875Z",
                updatedAt: "2014-09-03T11:39:38.216Z",
                id: "5406fd526d109ad149a1435f"
            },
            {
                filename: "test3_320x480.jpg",
                alt: "The last test image",
                device: "iphone",
                deviceColor: "#000",
                createdAt: "2014-09-03T11:43:09.599Z",
                updatedAt: "2014-09-03T11:43:09.599Z",
                id: "5406fecd6d109ad149a14361"
            }
        ],
        project: {
            title: "Project Alpha",
            subtitle: "A test project",
            image: "5406fd2d6d109ad149a1435e", // sails is populating this association fine
            position: 0,
            tabPos: 0,
            featured: false,
            createdAt: "2014-09-03T11:47:01.155Z",
            updatedAt: "2014-09-03T12:13:35.972Z",
            id: "5406ffb56d109ad149a14362"
        },
        copy: "Lorum ipsum",
        position: 0,
        createdAt: "2014-09-03T11:49:08.162Z",
        updatedAt: "2014-09-03T12:13:55.279Z",
        id: "540700346d109ad149a14363"
    }
]

The only thing I've overwritten is models at this stage, everything else is default.

Any help greatly appreciated.

mickyginger
  • 113
  • 11
  • possible duplicate of [Sails.js populate nested associations](http://stackoverflow.com/questions/23446484/sails-js-populate-nested-associations) – sgress454 Sep 03 '14 at 16:52
  • @sgress454 I had a look at that post and while it put me on the right lines, I'm left with the same issue of stringifying the resultant object: http://stackoverflow.com/questions/25142731/json-stringify-in-node-js-not-serializing-array-of-objects. I decided to user sails-ember-blueprints in the end: https://github.com/mphasize/sails-ember-blueprints – mickyginger Sep 06 '14 at 19:53

0 Answers0