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

Error while inserting a JSON array (if object contains the key "type") #838

Open
VxRain opened this issue Mar 27, 2024 · 0 comments
Open

Comments

@VxRain
Copy link

VxRain commented Mar 27, 2024

Hi, everyone,I encountered a strange phenomenon:
if the array object contains the key "type", an error occurs during insertion.

Reproduction Code

const postgres = require('postgres');
const sql = postgres('postgresql://xxxxxxxxxxxxxx', { max: 17 });

/**
AppPage DDL:
CREATE TABLE "public"."AppPage" (
  "id" int4 NOT NULL DEFAULT nextval('"AppPage_id_seq"'::regclass),
  "name" text COLLATE "pg_catalog"."default" NOT NULL,
  "desc" text COLLATE "pg_catalog"."default",
  "nodes" jsonb[] DEFAULT ARRAY[]::jsonb[],
  "pageSetId" int4,
  CONSTRAINT "AppPage_pkey" PRIMARY KEY ("id")
);
 */

async function main() {
  await sql`SELECT 1`;

  const title1 = 'test1';
  const desc1 = 'desc1';
  const nodes1 = [{ foo: 'bar' }];
  // ok
  await sql`INSERT INTO "AppPage" ("name", "desc", "nodes") VALUES (${title1}, ${desc1}, ${nodes1});`;

  const title2 = 'test2';
  const desc2 = 'desc2';
  const nodes2 = [{ type: 'bar' }];
  // If the object includes the type key, it will throw an error
  await sql`INSERT INTO "AppPage" ("name", "desc", "nodes") VALUES (${title2}, ${desc2}, ${nodes2});`;

  /**
  ERROR:
  ${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:237
      .replace(escapeBackslash, '\\\\')
      ^

  TypeError: Cannot read properties of undefined (reading 'replace')
      at arrayEscape (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:237:6)
      at ${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:264:15
      at Array.map (<anonymous>)
      at arraySerializer (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\types.js:255:19)
      at options.serializers.<computed> (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:761:45)
      at ${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:934:36
      at Array.forEach (<anonymous>)
      at Bind (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:928:16)
      at prepared (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:204:7)
      at toBuffer (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\connection.js:190:15)
      at cachedError (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\query.js:170:23)
      at new Query (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\query.js:36:24)
      at sql (${workspace}\node_modules\.pnpm\postgres@3.4.4\node_modules\postgres\cjs\src\index.js:112:11)
      at main (${workspace}\postgres.js:30:12)
 */
}

main();

Env

Nodejs: v20.11.1
Postgres.js: 3.4.4
PostgreSQL: 15.3

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

1 participant