1

我的问题是:

koa2 中间件'/info' ctx.res.body ------>front-end get对象没有 dataValues 和其他值。只有对象像{a:1,b:2}

在中间件中,我得到ser它有很多键值,比如{dataValue:{a:1,b:2},eviousDataValues:'xxx'}

为什么ko2中间件中的前端Object和'ser'不一样?

这是我的代码

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
})
4

1 回答 1

4

Sequelize 的响应始终是一个模型实例,其形式为。

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

如果您不需要实例,而只需要数据。您可以在查询中提供一个附加标志作为raw:true. 文档可在此处获得

这样做

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

将仅将数据作为纯 JS 对象返回。

于 2017-06-26T13:50:38.277 回答