How to get simple array in sequelize findAll()?

I’m trying to get all rows with specific ID in async/await style using sequelize. My code looks like:

UPDATED:

router.get('/', errorHandler(async (req, res, next) => {

    let domainsDB = await Domain.findAll({
            where: { accID: accID },
            attributes: [`adID`, `domain`, `status`]
    })

}

When I console.log(domainsDB) I see this:

[ 
     domains {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     domains {
...
...
...
]

The docs says it has to be an array in return. But console log shows this is an object. The data in this object in dataValues is correct. There are my domains. But I expect to get an array to work with.

How to get an array in return with async/await? Thank you for any help.

Answer

All you need is : raw : true ,

let domainsDB = await Domain.findAll({
    where: { accID: accID },
    attributes: [`adID`, `domain`, `status`],
    raw : true // <--- HERE
})

By default model queries returns the sequelize Object , to get the plain result we need to pass raw : true

Leave a Reply

Your email address will not be published. Required fields are marked *