Mohammad Ghifari
Mohammad Ghifari

Reputation: 25

Return data only without field name in Sequelize ORM

I am using Sequelize ORM to fetch data from db

.findAll({
    attributes: ['value'],
    where: {
        code: { [Op.like]: 'images_default_droplet_%' },
        is_published: 1
    }
})

and it return like this:

{"value":"data"}

can I just get the data only without the field name?

return that I want like this:

{"data"}

Upvotes: 2

Views: 1714

Answers (1)

doublesharp
doublesharp

Reputation: 27637

No, {"data"} isn't a valid JSON structure. You could convert it to an array of "datas" using Array.map(); passing raw: true to findAll() will return an array of plain JSON objects which is more efficient than converting to Model Instances first before mapping them to just the values you want.

const results = await Model.findAll({
  attributes: ['value'],
  where: {
    code: { [Op.like]: 'images_default_droplet_%' },
    is_published: 1
  },
  raw: true, // return array of plain JSON objects
});

// 'datas' is an array of ["data","data","data"...]
const datas = results.map((result) => result.value);

Upvotes: 1

Related Questions