我需要将json对象以及其他嵌套对象保存到我的postgres数据库中。我从用户浏览器收到的json。
我在AWS RDS上使用Postgres 11.5。在后端,我使用的是NodeJS(NestJS / TypeScript / TypeORM)。
我尝试使用JSON.stringigy()将数组的每个项目转换为字符串,但是无论如何我都收到错误UnhandledPromiseRejectionWarning: QueryFailedError: malformed array literal: "[{"name":"Oviedo, Asturias, Spain"},{"name":"Oviedo, Asturias, Spain"}]"
数组看起来像这样:
"waypoints": [
{
"name": "Oviedo, Asturias, Spain"
},
{
"name": "Oviedo, Asturias, Spain"
}
]
这是一个用于存储上方数组的字段
@Column({ type: 'jsonb', array: true })
waypoints: any[];
我分配这样的数据:
event.waypoints = eventData.waypoints;
// also I tried to do like this
event.waypoints = JSON.stringify([{ text: 'Some text' }, { text: 'Some text 2' }]);
现在我收到错误消息:
(node:12833) UnhandledPromiseRejectionWarning: QueryFailedError: malformed array literal: "[{"name":"Oviedo, Asturias, Spain"},{"name":"Oviedo, Asturias, Spain"}]"
at new QueryFailedError (/home/banha/Projects/train-time-server/node_modules/typeorm/error/QueryFailedError.js:11:28)
at Query.callback (/home/banha/Projects/train-time-server/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
at Query.handleError (/home/banha/Projects/train-time-server/node_modules/pg/lib/query.js:142:17)
at Connection.connectedErrorMessageHandler (/home/banha/Projects/train-time-server/node_modules/pg/lib/client.js:211:17)
at Connection.emit (events.js:209:13)
at Connection.EventEmitter.emit (domain.js:476:20)
at Socket.<anonymous> (/home/banha/Projects/train-time-server/node_modules/pg/lib/connection.js:126:12)
at Socket.emit (events.js:209:13)
at Socket.EventEmitter.emit (domain.js:476:20)
at addChunk (_stream_readable.js:305:12)
(node:12833) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12833) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.