简体   繁体   中英

Why does Sails v.0.10.x respond to POST to nested route with one-to-many association?

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'
    }
};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM