typeorm
typeorm copied to clipboard
getMany() returns undefined
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.
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.
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?
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.