Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 563
Nastro Avatar asked Dec 10 '22 04:12

Nastro


1 Answers

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

like image 95
Vivek Doshi Avatar answered Jan 07 '23 00:01

Vivek Doshi