You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to update a PostgreSQL column with the type JSON using the latest pg library (8.11.3) in Node.js, there is an unexpected behavior. Passing a JavaScript array directly as a parameter in a parameterized query results in an "invalid input syntax for type JSON" error. Manually converting the array to a JSON string using JSON.stringify resolves the error, as far as I understand the pg library should handle the conversion automatically.
const ff = [{"key1":"val1"},{"key2":"val2"}];
const query = {
text: 'UPDATE table SET column = $1 WHERE condition;',
values: [ff]
};
gives me:
2024-01-10T00:55:18.104Z; error; Query failed: 'invalid input syntax for type json', stack: error: invalid input syntax for type json
at Parser.parseErrorMessage (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket. (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17);
Using: $1::json doesn't help
This one works just fine:
const ff = [{"key1":"val1"},{"key2":"val2"}];
const jsonString = JSON.stringify(ff);
const query = {
text: 'UPDATE table SET column = $1 WHERE condition;',
values: [jsonString]
};
Could you please look.
Thank you.
The text was updated successfully, but these errors were encountered:
When attempting to update a PostgreSQL column with the type JSON using the latest pg library (8.11.3) in Node.js, there is an unexpected behavior. Passing a JavaScript array directly as a parameter in a parameterized query results in an "invalid input syntax for type JSON" error. Manually converting the array to a JSON string using JSON.stringify resolves the error, as far as I understand the pg library should handle the conversion automatically.
const ff = [{"key1":"val1"},{"key2":"val2"}];
const query = {
text: 'UPDATE table SET column = $1 WHERE condition;',
values: [ff]
};
gives me:
2024-01-10T00:55:18.104Z; error; Query failed: 'invalid input syntax for type json', stack: error: invalid input syntax for type json
at Parser.parseErrorMessage (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket. (C:\Users\user\Documents\Repositories\tmp\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17);
Using: $1::json doesn't help
This one works just fine:
const ff = [{"key1":"val1"},{"key2":"val2"}];
const jsonString = JSON.stringify(ff);
const query = {
text: 'UPDATE table SET column = $1 WHERE condition;',
values: [jsonString]
};
Could you please look.
Thank you.
The text was updated successfully, but these errors were encountered: