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