关于节点包“squelize",“dataValues"是什么意思? [英] about node package 'squelize',what is 'dataValues' 's mean?
问题描述
我的问题是:
koa2 中间件 '/info' ctx.res.body ------>front-end get
对象没有 dataValues 和其他值.只有像 这样的对象{a:1,b:2}
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}
在中间件中,我得到 ser
它有很多键值,例如 {dataValue:{a:1,b:2},eviousDataValues:'xxx'}
In middleware I get ser
it have so many key-value like {dataValue:{a:1,b:2},eviousDataValues:'xxx'}
为什么前端的Object和ko2中间件中的'ser'不一样?
Why is the front-end Object not the same as 'ser' in ko2 middleware?
这是我的代码
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
})
推荐答案
Sequelize 的响应始终是一个 Model Instance,其形式为.
The response from Sequelize is always an Model Instance, Which is of the form.
{
dataValues: [//All your data],
previousDataValues: [//Old Values],
... //many more properties
}
如果您不需要实例,而只需要数据.您可以在查询中提供一个额外的标志作为 raw:true
.文档可在这里
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
这样做
Model.A.findAll({where:{/*Some Conditions*/},raw:true})
将仅将数据作为普通 JS 对象返回.
will return just the data as a plain JS object.
这篇关于关于节点包“squelize",“dataValues"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!