【发布时间】: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