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

getMany() returns undefined #10562

Open
2 of 18 tasks
joachimbulow opened this issue Dec 21, 2023 · 4 comments
Open
2 of 18 tasks

getMany() returns undefined #10562

joachimbulow opened this issue Dec 21, 2023 · 4 comments

Comments

@joachimbulow
Copy link

Issue description

When using query builder we are are sometimes getting undefined returned from getMany() calls

Expected Behavior

If something goes wrong, e.g. a dropped connection or something similar, the library should throw an exception intead of returning undefined.

Or otherwise, if nothing goes wrong, just return an empty list.

Actual Behavior

In our error logging system we see null pointer errors for the following code

 const doubbleMatchesNotYetCreated = await manager
      .getRepository(entity)
      .createQueryBuilder()
      .where(query)
      .andWhere(
        () =>
          'some query that may or may not have results'
      )
      .orderBy('fooField')
      .getMany();

    if (!doubbleMatchesNotYetCreated.length) {
      return [];
    }

Because doubbleMatchesNotYetCreated is undefined.

Exactly why we do not know.

Steps to reproduce

We currently cannot provide these - errors seem sproadic. We will hotfix by making our result entities nullable with ?...

My Environment

Dependency Version
Operating System Debian 11.8
Node.js version v18.19.0
Typescript version 5.2.2
TypeORM version 0.3.17

Additional Context

We are using CockroachDB with the Postgres Driver

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

@alenap93
Copy link
Contributor

Executed behavior is to catch and error, not return undefined or an empty array, if you find the reason causing undefined value, write it here.

@wimagguc
Copy link

wimagguc commented May 9, 2024

We're seeing similar behavior with typeorm 0.3.20 (and node-postgres 8.11.3).
Specifically we've seen findOne() return undefined where we know that the row is present in the database, and where consecutive queries returned the object correctly.

The code is similar to what @joachimbulow shared:

const obj = await Obj.findOne({ where: { id } });
if (!obj) {
  throw new Error("...");
}

We've only ever logged three instances of this error out of a lot of queries, and always at times of heavy load.

@alenap93 our assumption too was that if there was a connection or a query error TypeOrm would have returned an error — but we know it didn't. But then what gives?

@wimagguc
Copy link

Seems to be related to this issue brianc/node-postgres#3174 in node-postgres — and in fact, using a script similar to the one suggested in brianc/node-postgres#3174 (comment) does trigger this error after a couple of retries.

@joachimbulow
Copy link
Author

Still alive btw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants