Sequelize exclude belongs-to-many mapping object

Viewed 9550

Is there a way when making a SequelizeJS query on an object, and including a relation which has a belongs-to-many association, to have the included property not return the association mapping object with the result?

i.e.:

Users.findAll({include: [{model: Role, as: 'roles'}]})

//yields objects of the following form
user: {
    username: 'test',
    roles: [
        {
           name: 'user',
           UserRoles: {userId: 1, roleId: 1} //<--I do not want this
        }
    ]        
}
3 Answers

enter image description here

export const getAllUsers = async (req, res) => {
    try {
        const users = await User.findAll({
            include: [
                {
                    model: Enrollment,
                    as: 'enrollments',
                    attributes:{exclude:['city']}
                }
            ],
            attributes: { exclude: ['password', 'refresh_token',] }
        })
        res.json(succesResponse(users))
    } catch (error) {
        res.json(errorResonse(error))
    }
}

im using this way to solve this issue

Related