0

In my app, I have two models Person and Room. I also have two controllers, PersonController and RoomController. I have added no custom routes, and I've overridden none of the boilerplate controller actions.

When I POST to, for example, /room/5/person, a person record is created and associated with room/5 as expected - great. However, the JSON returned in the response to this POST contains the Room and all People associated with the room, side-loaded in an array. I expected that Sails would return only the person created (or, at most, the newly created person and the room, if blueprints.populate: true).

What changes to I need to make to so that Sails returns only the Person created as a result of the POST?

Here's is my current code:

/api/models/Room.js

// Room.js

module.exports = {

    autosubscribe: ['update', 'add:people'],
    attributes: {
        name: 'string',

        // Has-many association
        // A room can have many people
        people: {
            collection: 'person',
            via: 'room'
        }
    }
};

/api/models/Person.js

module.exports = {

    autosubscribe: ['update'],
    attributes: {
        name: 'string',

        // Belongs-to association
        // A person can belong to only one room
        room: {
            model: 'room'
        }
    }
};

/api/controllers/RoomController.js

module.exports = {

};

/api/controllers/PersonController.js

module.exports = {

};

/config/routes.js

module.exports.routes = {

    '/': {
        view: 'homepage'
    }
};
kevinsapp
  • 374
  • 5
  • 11
  • Have you looked at the [source](https://github.com/balderdashy/sails/tree/master/lib/hooks/blueprints/actions) code for the blueprints? If there is no way to config, it may be worthwhile write your own blueprint [override](http://sailsjs.org/#/documentation/reference/blueprint-api?q=overriding-blueprints) – Xinzz Aug 12 '14 at 03:54
  • Thanks for the helpful comment @Xinzz. I'll look into this option. In the meantime, I'll refactor my request per sgress454's answer. – kevinsapp Aug 12 '14 at 15:19

1 Answers1

1

This is by design; you're using the "add person to room" blueprint, so it's assumed you care about the state of the room. If you just want the person returned, use the "create person" blueprint, and supply the room ID in the payload:

POST /person

{
    name: "John Doe",
    room: 5
}

This will still send out pubsub notifications about a person being added to room 5.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • Thanks for your answer @sgress454. I'll switch to this approach since it's apparently the "Sails way". If I did however want to change the semantics such that `POST /room/5/person` did return the created person rather than the room, would you recommend overriding the `add.js` blueprint? – kevinsapp Aug 12 '14 at 15:24
  • 1
    Overriding the blueprint is a good solution if you want to change the behavior for all models. If you just want to change it for `Room`, you can just add an `add` method to your **RoomController.js**. – sgress454 Aug 12 '14 at 16:14