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
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:
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.
Finally I found answer after searching a lot. you should do something like this
source: github issue
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:
Additionally if you just want to print out the object you can use the
.toJSON
method.Yes you can
would return just the data and not the model instance
For nested entities this stupid workaround works for me:
Somehow
JSON.stringify(row, null, 4)
removes all the extra stuff and pretends thedataValues
property of the object is the object itself. Then theJSON.parse(...)
puts the object back together.EDIT:
so apparently I'm new to typescript so I didn't need this at all. the
console.log(row)
printing huge object got me confused. I miss good ol' c# times :'(The problem occurs only when I log it using:
If I save it to a variable, I can directly access objects inside without using "dataValues"