【问题标题】:Storing objects in JSONB column using Postgres/Knex使用 Postgres/Knex 在 JSONB 列中存储对象
【发布时间】:2020-04-20 18:17:18
【问题描述】:

我在后端创建了一个图表来存储与图表相关的数据。以下是 Knex 迁移文件中的架构:

exports.up = function(knex) {
  return (
      knex.schema
        .createTable('users', tbl => {
            tbl.increments();
            tbl.string('username', 255).notNullable();
            tbl.string('password', 255).notNullable();
            tbl.string('name', 255).notNullable();
            tbl.string('email', 255).unique().notNullable();
        })
        .createTable('graphs', tbl => {
          tbl.increments();
          tbl.string('graph_name', 255).notNullable();
          tbl.jsonb('graph_info').notNullable();
          tbl
            .integer('user_id')
            .unsigned()
            .notNullable()
            .references('id')
            .inTable('users')
            .onDelete('CASCADE')
            .onUpdate('CASCADE');
        })
  )
};

这是我试图存储在数据库的 jsonb 列中的数据类型示例:

{
  labels: ['Axis1', 'Axis2', 'Axis3'],
  datasets: [
        {
          label: 'Dataset1',
          borderDash: [0, 0],
          backgroundColor: '#fff',
          data: [25, 14, 22],
        },
      ],
  title: 'Graph1',
}

现在这是我尝试通过 Postman 发送的请求:

{
    "graph_name": "test10",
    "graph_info": "{
        labels: ['Axis1', 'Axis2', 'Axis3'],
        datasets: [
            {
              label: 'Dataset1',
              borderDash: [0, 0],
              backgroundColor: '#fff',
              data: [25, 14, 22],
            },
                ],
            title: 'Graph1'
        }",
    "user_id": "1"
}

我收到以下错误:SyntaxError: Unexpected token in JSON at position 44。当我试图弄清楚发生了什么时,我偶然发现了这篇文章:

sending nested json object using postman

我的 Content-Type 标头设置为 application/json,但 Postman 没有给我发送错误请求的信号。我不确定是什么问题。

我也有可能以错误的方式思考这个问题。当我查看 Postgres 文档时,在我看来,存储我要存储的对象类型的最佳方法是使用 jsonb 列。但如果不是这样,我愿意接受建议。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js postgresql postman knex.js


    【解决方案1】:

    从结果中graph_info看,不是合法的JSON,而是一个包含js对象的字符串。

    当您将数据保存到 JSONB 列时,您需要在对象上应用JSON.stringify 方法。

    例如这样:

    {
        "graph_name": "test10",
        "graph_info": JSON.stringify({
            labels: ['Axis1', 'Axis2', 'Axis3'],
            datasets: [
                {
                  label: 'Dataset1',
                  borderDash: [0, 0],
                  backgroundColor: '#fff',
                  data: [25, 14, 22],
                },
            ],
            title: 'Graph1'
        }),
        "user_id": "1"
    }
    

    【讨论】:

    • 感谢您回复我。我正在与其他正在构建前端的开发人员合作。有没有办法可以使用 Postman 进行测试?我知道我可以让前端开发人员在发送图形数据之前对其进行字符串化。但我也想排除故障。
    • 只检查你在服务器上的内容,通常服务器默认解析json body,因此你需要将它转换回字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2019-08-13
    • 1970-01-01
    • 2019-07-13
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多