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

标签 json node.js express sequelize.js

使用 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/

相关文章:

ios - 如何从json响应中获取值并在相应的值上传递相应的索引ios

node.js - 带有 express 的socket.io

javascript - 从 BrowserWindow Electron 类扩展时出错

node.js - 如何在快速 Handlebars 上实现条件?

javascript - d3 等值线队列和填充问​​题

json - 使用异步线程时, Realm 数据库大小变为 x100

java - 反序列化多态 JSON 字符串

node.js - Sequelize js上更新和删除的问题

node.js - 为什么我的中间件不检查电子邮件是否已存在?

api - 当params描述关系时如何将查询参数传递给sequelize