egg-sequelize的查询返回json的问题

发布于 2022-09-12 03:12:15 字数 3158 浏览 20 评论 0

我用findAll查询了分类的数据

const list = await model.findAll()

打印list如下:

[
  Category {
    dataValues: {
      id: 1,
      name: 'js',
      parentId: null,
      createdAt: '2020-03-15 12:35:49',
      updatedAt: '2020-03-15 12:35:49',
      deletedAt: null,
      children: [Array]
    },
    _previousDataValues: {
      id: 1,
      name: 'js',
      parentId: null,
      createdAt: '2020-03-15 12:35:49',
      updatedAt: '2020-03-15 12:35:49',
      deletedAt: null,
      children: [Array]
    },
    _changed: {},
    _modelOptions: {
      timestamps: true,
      validate: {},
      freezeTableName: false,
      underscored: false,
      paranoid: true,
      rejectOnEmpty: false,
      whereCollection: [Object],
      schema: null,
      schemaDelimiter: '',
      defaultScope: {},
      scopes: {},
      indexes: [],
      name: [Object],
      omitNull: false,
      sequelize: [Sequelize],
      hooks: {}
    },
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
    },
    isNewRecord: false,
    children: [ [Category] ]
  },
  Category {
    dataValues: {
      id: 3,
      name: 'css',
      parentId: null,
      createdAt: '2020-06-12 01:00:39',
      updatedAt: '2020-06-12 01:00:39',
      deletedAt: null,
      children: []
    },
    _previousDataValues: {
      id: 3,
      name: 'css',
      parentId: null,
      createdAt: '2020-06-12 01:00:39',
      updatedAt: '2020-06-12 01:00:39',
      deletedAt: null,
      children: []
    },
    _changed: {},
    _modelOptions: {
      timestamps: true,
      validate: {},
      freezeTableName: false,
      underscored: false,
      paranoid: true,
      rejectOnEmpty: false,
      whereCollection: [Object],
      schema: null,
      schemaDelimiter: '',
      defaultScope: {},
      scopes: {},
      indexes: [],
      name: [Object],
      omitNull: false,
      sequelize: [Sequelize],
      hooks: {}
    },
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
    },
    isNewRecord: false,
    children: []
  }
]

然后我把list给ctx.body返回

ctx.body = list

打印的response中,list是正确的json,而不是上面的对象,json如下:

\[

{

"id": 1,

"name": "js",

"parentId": null,

"createdAt": "2020-03-15 12:35:49",

"updatedAt": "2020-03-15 12:35:49",

"deletedAt": null,

"children": \[

{

"id": 2,

"name": "ts",

"parentId": 1,

"createdAt": "2020-06-12 01:00:29",

"updatedAt": "2020-06-12 01:00:29",

"deletedAt": null,

"children": \[\],

"articles": \[\]

}

\]

},

{

"id": 3,

"name": "css",

"parentId": null,

"createdAt": "2020-06-12 01:00:39",

"updatedAt": "2020-06-12 01:00:39",

"deletedAt": null,

"children": \[\]

}

\]

为什么会不一样呢?还是sequelize对这个封装对象做了什么处理?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文