How to change the Sequelize response object?

Asked

Viewed 28 times

0

I would like to know how to change the answer object in sequelize findAll.

I used the code below to make the change but I do not know if in Sequelize itself can do.

then(chars => {
        chars.map(teste => console.log('TESTE', teste.dataValues))
        res.status(200).json({
            data: chars.map(char => ({
                ...char.dataValues,
                attributes: char.attributes.map(attribute => {
                    const {char_attribute: {value}, dataValues: {char_attribute, ...rest}} = attribute
                    return {...rest, value}
                })
            })),
            success: true,
            message: ''
        })

inserir a descrição da imagem aqui

  • What name do you want for the field? I think there’s a way to make Sequelize to stay flat, guy "char_attribute": 10, but changing the name so I think just mapping it. Just to confirm, this field is an association right?

  • Hi. Yes is an association. I don’t want to change the name, I just want to take the "char_attribute": {"value": 10} and put "value" directly: 10.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.