V
V
Vladimir2018-02-08 17:39:05
MySQL
Vladimir, 2018-02-08 17:39:05

How to get clean #sequelize data?

I use to work with mysql Sequelize. but when fetching data, it returns its object, like this:

spoiler
top {
  dataValues:
   { id: 4,
     is_bool: true,
     value: 'some value',
     order: 4,
     mid: [],
     bot: [ [Object], [Object], [Object], [Object] ] },
  _previousDataValues:
   { id: 4,
     is_bool: true,
     value: 'some value',
     order: 4,
     mid: [],
     bot: [ [Object], [Object], [Object], [Object] ] },
  _changed: {},
  _modelOptions:
   { timestamps: false,
     validate: {},
     freezeTableName: true,
     underscored: true,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: [Array] },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     indexes: [],
     name: { plural: 'tops', singular: 'top' },
     omitNull: false,
     version: false,
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [Object],
        queryInterface: [Object],
        models: [Object],
        modelManager: [Object],
        connectionManager: [Object],
        importCache: {},
        test: [Object] },
     hooks: {},
     uniqueKeys: {} },
  _options:
   { isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
     include: [ [Object], [Object] ],
     includeNames: [ 'mid', 'bot' ],
     includeMap: { mid: [Object], bot: [Object] },
     includeValidated: true,
     attributes: [ 'id', 'is_bool', 'value', 'order' ],
     raw: true },
  __eagerlyLoadedAssociations: [],
  isNewRecord: false,
  mid: [],
  bot:
   [ fields_item {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     fields_item {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     fields_item {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false },
     fields_item {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       __eagerlyLoadedAssociations: [],
       isNewRecord: false } ] }

if you use it without complex queries, then it’s not difficult to go through a loop and pick up dataValues, but with an increase in inclusions and complication of queries, everything becomes different.
There is an important point: when data is transferred to the client, they become clean. So, where all the same there is a necessary mechanism. But I didn't get anything...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-02-08
@MegaBatz

const model = await Model.findById(...);

const data = model.toJSON();

console.log(data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question