1

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
})
Frits
  • 7,341
  • 10
  • 42
  • 60
zii lee
  • 13
  • 1
  • 3

1 Answers1

5

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.

Shivam
  • 3,462
  • 1
  • 15
  • 20
  • You could mark it as answered for anyone else who stumbles here – Shivam Jun 27 '17 at 16:47
  • and I checked Sequelize instance.__proto__ , i find a function , a name toJSON function,it return a Object, JSON.stringfy() is find this Object and then retrun this Object,don't return JSON.stringfy(mydata),so brother ,this is the key to my question,thank you – zii lee Jun 27 '17 at 16:56