gpt4 book ai didi

json - 转换为 json 时序列化下划线/蛇形大小写

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:34 24 4
gpt4 key购买 nike

使用 Node 和 Sequelize,是否可以定义一个模型,该模型使用驼峰命名法作为属性,但在转换为 json 时使用下划线/蛇形命名法?

目前;

var User = seq.define('user', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
shortLivedToken: {
type: Sequelize.STRING,
field: 'short_lived_token'
},
longLivedToken: {
type: Sequelize.STRING,
field: 'long_lived_token'
}
}, {
underscored: true,
underscoredAll: true,
});

调用response.json(user)时变成

{
"id": null,
"shortLivedToken": "abcde",
"longLivedToken": "fghji",
"updated_at": "2015-11-21T18:32:20.181Z",
"created_at": "2015-11-21T18:32:20.181Z"
}

我希望它是

{
"id": null,
"short_lived_token": "abcde",
"long_lived_token": "fghij",
"updated_at": "2015-11-21T18:32:20.181Z",
"created_at": "2015-11-21T18:32:20.181Z"
}

有什么想法吗?

最佳答案

Not possible. The attribute names are used for getting values, toJSON uses methods for getting values. Overwrite toJSON or don't use camel cased attribute names.

https://github.com/sequelize/sequelize/issues/4906

关于json - 转换为 json 时序列化下划线/蛇形大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com