reactjs 如何从对象获取数据

vyswwuz2  于 2023-01-17  发布在  React
关注(0)|答案(1)|浏览(147)

如何从对象中获取数据?我需要从dataValues中获取数据 然后写下来

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'
    ]
  },
}
eimct9ow

eimct9ow1#

您可以通过user.id、用户名或解构直接访问数据:

const {id,firstName} = user

另一个选项是放置原始:没错。

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

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

相关问题