Skip to content
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

pg-protocol not support js-object value #2864

Closed
transtone opened this issue Nov 22, 2022 · 4 comments
Closed

pg-protocol not support js-object value #2864

transtone opened this issue Nov 22, 2022 · 4 comments

Comments

@transtone
Copy link

transtone commented Nov 22, 2022

"aa": [
	{"bb": "cc"}
]

this will treat as a error json!

error: invalid input syntax for type json
    at Parser.parseErrorMessage (path\node_modules\pg-protocol\dist\parser.js:287:98)
    at Parser.handlePacket (path\node_modules\pg-protocol\dist\parser.js:126:29)
    at Parser.parse (path\node_modules\pg-protocol\dist\parser.js:39:38)
    at Socket.<anonymous> (path\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (node:events:526: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) {
  length: 169,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Expected ":", but found ",".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: 'JSON data, line 1: {"{\\"bb\\":\\"cc\\"}",...',
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'json.c',
  line: '1203',
  routine: 'report_parse_error'

if insert or update a jsonb field with js object, then will get the error.

must use JSON.stringify(someObject).

@brianc
Copy link
Owner

brianc commented Nov 22, 2022

thanks for the report - can you include a bit more detail on how you made this bug trigger?

@transtone
Copy link
Author

transtone commented Nov 22, 2022

CREATE TABLE public.test (
	id varchar NOT NULL DEFAULT uuid_generate_v4(),
	"data" jsonb NULL,
);
const insertData= [{"bb": "cc"}]

const insertQuery = {
  text: 'INSERT INTO public.test (data) VALUES ($1) ',
  values: [ insertData ]
}
const client = await pool.connect()
try {
    await client.query('BEGIN')
    await client.query(insertQuery)
    await client.query('COMMIT')
 } catch (e) {
    await client.query('ROLLBACK')
    console.log(e)
 }
client.release()

then will get the error.

it must use values: [JSON.stringify(insertData)] , then everything goes ok.

@transtone transtone changed the title pg-protocol bug pg-protocol not support js-object value Nov 22, 2022
@transtone
Copy link
Author

in the doc:
https://node-postgres.com/features/types#uuid--json--jsonb

const createTableText = `
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
 
CREATE TEMP TABLE IF NOT EXISTS users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  data JSONB
);
`
// create our temp table
await client.query(createTableText)
 
const newUser = [{ email: '[email protected]' }]
// create a new user
await client.query('INSERT INTO users(data) VALUES($1)', [newUser])
 
const { rows } = await client.query('SELECT * FROM users')

if the newUser is Array, will triggle the error.

@transtone transtone reopened this Nov 22, 2022
@charmander
Copy link
Collaborator

See #2012.

@charmander charmander closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2022
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

3 participants