最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get simple array in sequelize findAll()? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 4, 2018 at 11:58 Nastro asked Dec 4, 2018 at 11:36 NastroNastro 1,7698 gold badges24 silver badges41 bronze badges 6
  • Where do you console.log ? is it immediate call after let domainsDB? – Mehrdad Shokri Commented Dec 4, 2018 at 11:46
  • @MehiShokri Yes, it's – Nastro Commented Dec 4, 2018 at 11:48
  • It's because you're getting the created json object. not the answer. – Mehrdad Shokri Commented Dec 4, 2018 at 11:51
  • Are you sure you're not getting an array? Your return result starts with [ and ends with ] – Khauri Commented Dec 4, 2018 at 12:05
  • @KhauriMcClain Yes, it's an array. I expected more convenient array I think. But now I see promise returns the same array as async/await. Probably it's the expected behavior of sequelize – Nastro Commented Dec 4, 2018 at 12:11
 |  Show 1 more comment

2 Answers 2

Reset to default 18

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

await should be wrapped in a function with async key in it.

A correct code snippet would look like this:

async fetchData(accID) {
  return await Domain.findAll({
      where: { accID: accID },
      attributes: [`adID`, `domain`, `status`]
  })
}

let data = fetchData(1);
console.log(data);

Alternative approaches are: 1) using generator functions 2) promises.

They're basically the same thing in a manner that it makes your synchronous code act as asynchronous.

发布评论

评论列表(0)

  1. 暂无评论