typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

getMany() returns undefined

Open joachimbulow opened this issue 1 year ago • 2 comments

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
  • [X] cockroachdb
  • [ ] cordova
  • [ ] expo
  • [ ] mongodb
  • [ ] mysql
  • [ ] nativescript
  • [ ] oracle
  • [X] 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.

joachimbulow avatar Dec 21 '23 09:12 joachimbulow

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.

alenap93 avatar Dec 22 '23 12:12 alenap93

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 avatar May 09 '24 13:05 wimagguc

Seems to be related to this issue https://github.com/brianc/node-postgres/issues/3174 in node-postgres — and in fact, using a script similar to the one suggested in https://github.com/brianc/node-postgres/issues/3174#issuecomment-2127299540 does trigger this error after a couple of retries.

wimagguc avatar May 24 '24 15:05 wimagguc