Home > database >  How to get data from an object
How to get data from an object

Time:01-22

How to get data from an object? I need to get data from dataValues ​​and write it down

const user =  User.findOne({
}).then(e=>console.log(e))
User {
  dataValues: {
    id: 1,
    firstName: 'Мен',
    lastName: 'Bezrukov',
    login: 'qqq',
    password: '1234',
    role: 'admin',
    ip: '12345',
    descipt: 'developer',
    o_sebe: 'top man',
    soc_set: 'vk',
    age: '17',
    likes__foto: '5',
    coment__foto: null,
    createdAt: 2023-01-15T09:06:39.000Z,
    updatedAt: 2023-01-15T09:07:00.000Z
  },
  _previousDataValues: {
    id: 1,
    firstName: 'Мен',
    lastName: 'Bezrukov',
    login: 'qqq',
    password: '1234',
    role: 'admin',
    ip: '12345',
    descipt: 'developer',
    o_sebe: 'top man',
    soc_set: 'vk',
    age: '17',
    likes__foto: '5',
    coment__foto: null,
    createdAt: 2023-01-15T09:06:39.000Z,
    updatedAt: 2023-01-15T09:07:00.000Z
  },
  uniqno: 1,
  _changed: Set(0) {},
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    raw: true,
    attributes: [
      'id',           'firstName',
      'lastName',     'login',
      'password',     'role',
      'ip',           'descipt',
      'o_sebe',       'soc_set',
      'age',          'likes__foto',
      'coment__foto', 'createdAt',
      'updatedAt'
    ]
  },
}

CodePudding user response:

You can access directly the data by user.id, user.firstName or destructuring:

const {id,firstName} = user

another options is putting raw: true.

User.findOne({ where: {firstname: "some name"}, 
raw: true
})

This gives you directly an object with only the data instead of an sequelize instance.
  • Related