Skip to content

TypeError: Cannot read properties of undefined (reading 'replace') #923

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
alexgleason opened this issue Aug 2, 2024 · 14 comments
Closed

Comments

@alexgleason
Copy link

I'm getting some kind of error, but the error handler itself has an error, so I'm not sure what the error is. 😃

error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'replace')
      stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
                                               ^
    at queryError (https://deno.land/x/postgresjs@v3.4.4/src/connection.js:392:48)
    at errored (https://deno.land/x/postgresjs@v3.4.4/src/connection.js:387:17)
    at error (https://deno.land/x/postgresjs@v3.4.4/src/connection.js:379:5)
    at https://deno.land/x/postgresjs@v3.4.4/polyfills.js:138:30
    at Array.forEach (<anonymous>)
    at call (https://deno.land/x/postgresjs@v3.4.4/polyfills.js:138:16)
    at error (https://deno.land/x/postgresjs@v3.4.4/polyfills.js:131:5)
    at eventLoopTick (ext:core/01_core.js:168:7)
@alexgleason
Copy link
Author

alexgleason commented Aug 2, 2024

So I edited the code manually and logged it. err is:

InvalidData: invalid peer certificate: UnknownIssuer
    at async TlsConn.read (ext:deno_net/01_net.js:150:15)
    at async success (https://raw.githubusercontent.com/alexgleason/postgres.js/51ac395234089d5b7806c471830a1a472958fb06/deno/polyfills.js:97:56) {
  name: "InvalidData"
}

And the value of query is... true ???

EDIT: It's because query is being set to true here:

initial = query || true

@marbemac
Copy link

Running into this as well

@patrickReiis
Copy link

I'm also having this problem

@dhardtke
Copy link

dhardtke commented Nov 7, 2024

We are also seeing this error in our projects:

TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (/usr/src/app/node_modules/postgres/cjs/src/connection.js:389:48)
    at errored (/usr/src/app/node_modules/postgres/cjs/src/connection.js:383:14)
    at TLSSocket.data (/usr/src/app/node_modules/postgres/cjs/src/connection.js:318:9)
    at TLSSocket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:191:23)

I am not convinced it is an issue with the initial query since /usr/src/app/node_modules/postgres/cjs/src/connection.js:383:14 points to

query && queryError(query, err)

It is very problematic that we don't see the actual error. It does not happen on startup but when our NodeJS backend is under high load.

@arenddeboer
Copy link

arenddeboer commented Dec 19, 2024

I'm getting this error with target_session_attrs: 'primary' where the replica is down and the replica as first host in a multi host connection string. Will try to dig deeper.

@alexgleason
Copy link
Author

If you are using Deno you can work around this by switching to my fork with:

import postgres from "https://gitlab.com/soapbox-pub/postgres.js/-/raw/e79d7d2039446fbf7a37d4eca0d17e94a94b8b53/deno/mod.js";

I've been using this fork in a high workload application for 3 or 4 months now.

It's just the patch applied from #944

If you're on Node, perhaps edit the file in node_modules manually to apply the change from #944. That will at least give you the correct error message, which you may only need temporarily to solve the issue.

@arenddeboer
Copy link

Thanks @alexgleason
I'm on node.js and the timeout seems to trigger connectTimedOut() which calls errored() which calls queryError().
As this only happens during startup and the reconnecting mechanism to other hosts in the host string works at runtime, I will resort to sorting the host string with the primary first before initializing.

@porsager
Copy link
Owner

Fixed in e8bb3b8

@abdelfattahradwan
Copy link

I am still getting the same error after updating to 3.4.6.

file:///PROJECT_PATH/node_modules/postgres/src/connection.js:392
      stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
                                               ^

TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///PROJECT_PATH/node_modules/postgres/src/connection.js:392:48)
    at errored (file:///PROJECT_PATH/node_modules/postgres/src/connection.js:383:14)
    at Socket.data (file:///PROJECT_PATH/node_modules/postgres/src/connection.js:318:9)
    at Socket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
    at Readable.push (node:internal/streams/readable:392:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:189:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

Node.js v22.15.1

The error above gets printed, and the app crashes.

@porsager
Copy link
Owner

Thank you for the quick check @abdelfattahradwan

Can you show some code that causes this?

@abdelfattahradwan
Copy link

Thank you for the quick check @abdelfattahradwan

Can you show some code that causes this?

I have the following code in a db.ts module:

export const psql = postgres({
  host: env.DB_HOST,
  port: Number.parseInt(env.DB_PORT, 10),
  user: env.DB_USER,
  password: env.POSTGRES_PASSWORD,
  database: env.DB_NAME,
});

const dialect = new PostgresJSDialect({
  postgres: psql,
});

export const kysely = new Kysely<Record<string, Record<string, unknown>>>({
  dialect,
});

And upon starting my app, I run the following query:

export async function getRecurringPayments(
  options: typeof GetRecurringPaymentsOptionsSchema.infer,
) {
  let query = kysely
    .selectFrom("public.recurring_payments_view")
    .select([
      "public.recurring_payments_view.uuid",
      "public.recurring_payments_view.name",
      "public.recurring_payments_view.description",
      "public.recurring_payments_view.start_date",
      "public.recurring_payments_view.frequency",
      "public.recurring_payments_view.next_charge_date",
      "public.recurring_payments_view.days_until_next_charge",
      "public.recurring_payments_view.normalized_days_until_next_charge",
      "public.recurring_payments_view.cost",
      "public.recurring_payments_view.currency",
      "public.recurring_payments_view.effective_local_cost",
      "public.recurring_payments_view.is_active",
      "public.recurring_payments_view.payment_type",
      "public.recurring_payments_view.apply_foreign_exchange_markup",
    ]);

  if (options.sortBy) {
    query = query.orderBy(options.sortBy, options.sortingOrder ?? "asc");
  }

  const rows = await query.execute();

  return GetRecurringPaymentsResultSchema.assert(rows);
}

Once that (or any other query is executed) the error is printed and app the crashes 🫠

@porsager
Copy link
Owner

Perfect ! I can reproduce here now - looking into it

@abdelfattahradwan
Copy link

@porsager I just tested 3.4.7. The issue seems to have been resolved! Thank you!

@porsager
Copy link
Owner

Thanks for the help on this @abdelfattahradwan .. fixed and released ;)

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

Successfully merging a pull request may close this issue.

8 participants
@marbemac @porsager @dhardtke @alexgleason @arenddeboer @abdelfattahradwan @patrickReiis and others