Pesquisa de site

node.js - QueryFailedError: literal de array malformado quando estou tentando inserir um array JSON no Postgres usando TypeORM


Preciso salvar um objeto json com outros objetos aninhados no meu banco de dados postgres. O json que estou recebendo do navegador do usuário.

Estou usando Postgres 11.5 no AWS RDS. No backend, estou usando NodeJS (NestJS/TypeScript/TypeORM).

Tentei converter cada item do array para string usando JSON.stringify(), mas mesmo assim estou recebendo o erro UnhandledPromiseRejectionWarning: QueryFailedError: malformed array literal: "[{"name":"Oviedo, Asturias, Spain"},{"name":"Oviedo, Asturias, Spain"}]"

O array se parece com isto:

"waypoints": [
        {
            "name": "Oviedo, Asturias, Spain"
        },
        {
            "name": "Oviedo, Asturias, Spain"
        }
    ]

Este é um campo para armazenar o array acima

@Column({ type: 'jsonb', array: true })
    waypoints: any[];

E eu atribuo dados assim:

  event.waypoints = eventData.waypoints;

  // also I tried to do like this
  event.waypoints = JSON.stringify([{ text: 'Some text' }, { text: 'Some text 2' }]);

Neste momento, estou recebendo o erro:

(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.

Artigos relacionados