简体   繁体   English

如何从 object 获取数据

[英]How to get data from an object

How to get data from an object?如何从 object 获取数据? I need to get data from dataValues and write it down我需要从 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'
    ]
  },
}

You can access directly the data by user.id, user.firstName or destructuring:您可以通过 user.id、user.firstName 或解构直接访问数据:

const {id,firstName} = user

another options is putting raw: true.另一个选项是 put 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM