Skip to content

Cannot use array in in a parameterized query. #3125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pimtchenkov opened this issue Jan 10, 2024 · 1 comment
Closed

Cannot use array in in a parameterized query. #3125

pimtchenkov opened this issue Jan 10, 2024 · 1 comment

Comments

@pimtchenkov
Copy link

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.

@charmander
Copy link
Collaborator

Duplicate of #442. Use JSON.stringify on the parameter you’d like to pass as JSON.

@charmander charmander closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants