Get only dataValues from Sequelize ORM

node.jssequelize.jsSequelize Cli

node.js Problem Overview

I'm using the sequelize ORM to fetch data from a PSQL DB. However, when I retrieve something, a whole bunch of data is given. The only data I want is inside 'dataValues'. Of course, I can use object.dataValues. But, is there any other good solutions?

I'm using Sequelize 4.10

node.js Solutions

Solution 1 - node.js

Yes you can

 raw: true,
 //Other parameters

would return just the data and not the model instance

Solution 2 - node.js

Sequelize wraps all it's return values in a virtual object that contains meta data. If you have an object and you just want the undecorated data values, you can unwrap them like so:

Model.findById(1).then(data => {
  console.log(data.get({ plain: true }));

Additionally if you just want to print out the object you can use the .toJSON method.

Model.findById(1).then(data => {

Solution 3 - node.js

Finally I found answer after searching a lot. you should do something like this

const users = await db.users.findAll({})
   .map(el => el.get({ plain: true })) // add this line to code

source: github issue

Solution 4 - node.js

To clarify Masoud Tavakkoli's answer (which is not immediately clear on that github answer): using element.get({ plain: true }) returns an array of objects with each attribute key:value pairs.

If you just want an array of one specific attribute's values (eg user ids) instead of objects, you can use something like this:

const users = await User.findAll({
    attributes: ["id"], 
    where: {} // Your filters here
}).map(u => u.get("id")) // [1,2,3]

Nika Kasradze's answer actually achieves the same outcome as a middle-step; using the JSON stringifier generates the same array output. It's possible this is faster than mapping, but I'm not sure.

const users = await User.findAll({
    attributes: ["id"], 
    where: {} // Your filters here
const userIds = JSON.stringify(users)) // [1,2,3]

Solution 5 - node.js

This is how I solved mine

    let rows = await database.Book.findAll(options);
    rows = JSON.stringify(rows);
    rows = JSON.parse(rows);

Note that the query has 'include childModel' i could've used 'raw:true' if it just one model. Stringifying the result clears out the '_previousDataValues' e.t.c and give you plain object, now parse the stringified obect back to json. Took me a long time before I could figure this out.

Solution 6 - node.js

The problem occurs only when I log it using:

> console.log(Model.findAll());

If I save it to a variable, I can directly access objects inside without using "dataValues"


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGijo VargheseView Question on Stackoverflow
Solution 1 - node.jsShivamView Answer on Stackoverflow
Solution 2 - node.jsC DeuterView Answer on Stackoverflow
Solution 3 - node.jsMasoud TavakkoliView Answer on Stackoverflow
Solution 4 - node.jsdefraggledView Answer on Stackoverflow
Solution 5 - node.jsAhmed AdewaleView Answer on Stackoverflow
Solution 6 - node.jsGijo VargheseView Answer on Stackoverflow