about node package 'squelize',what is 'dataValues' 's mean?

我们两清 提交于 2021-01-28 06:45:57

问题


My question is:

koa2 middleware '/info' ctx.res.body ------>front-end get Object doesn't have dataValues and other value .only have object like {a:1,b:2}

In middleware I get ser it have so many key-value like {dataValue:{a:1,b:2},eviousDataValues:'xxx'}

Why is the front-end Object not the same as 'ser' in ko2 middleware?

this is my code

let sequelize = new Sequelize('test', sqlOpt.name, sqlOpt.pwd, {
host: sqlOpt.host,
dialect: 'mysql',
port: sqlOpt.port,
pool: {
max: 5000,
min: 0,
idle: 10000
}})
let api = sequelize.define(
'api', {
  id: {
    type: Sequelize.STRING,
    primaryKey: true
  },
  name: Sequelize.STRING,
  method: Sequelize.STRING,
  url: Sequelize.STRING,
  description: Sequelize.STRING,
  createTime: Sequelize.INTEGER,
  did: Sequelize.STRING,
  status: Sequelize.INTEGER,
  content_type: Sequelize.INTEGER
}, {
  freezeTableName: true,
  tableName: 'apilist',
  timestamps: false
})
module.exports = {
searchbyDid(params) {
  return api.findAll({
  where: {
    did: params.id
  }
})}}

router.get('/info', async ctx => {
  let ser = await ser_m.searchAll()
  for (let val of ser) {
    val.dataValues.item = await api_m.searchbyDid({
      id: val.id
    })
  }
  ctx.response.body = ser
})

回答1:


The response from Sequelize is always an Model Instance, Which is of the form.

{
  dataValues: [//All your data],
  previousDataValues: [//Old Values],
  ... //many more properties
}

If you don't need the instance, and just want the data. You can supply an additional flag in your query as raw:true. Docs are available here

So doing

Model.A.findAll({where:{/*Some Conditions*/},raw:true})

will return just the data as a plain JS object.



来源:https://stackoverflow.com/questions/44753699/about-node-package-squelize-what-is-datavalues-s-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!