TypeORM findOne returns a row you did not ask for
findOneBy({ id }) returns a user that is not the one requestedShort answer
Before TypeORM 1.0, a null or undefined value inside where was silently skipped. If the id comes in as undefined, the filter vanishes and findOne returns the first row. Guard the value before the query, and on 1.0 keep the default, which throws. The old findOne(id) form was also removed in 0.3.
Fix it fast
1Find out which cause you have
Log the value you pass into where, right before the call. If it is undefined, the cause is the first one below.
console.log("id:", id, typeof id) const user = await userRepo.findOneBy({ id })2Match what you see, then jump to its fix
- 1
id prints undefined and the first row of the table comes backThe where value is undefined, so the condition is skippedFix - 2
Wrong or missing results after upgrading from 0.2 to 0.3Code written for the old findOne(id) or findOne() formFix - 3
TypeORMError about a null or undefined value in whereYou upgraded to 1.0 and the same code now throwsFix
- 1
What does the error look like?
findOneBy({ id }) returns a user that is not the one requestedfindOne() with no arguments or findOne(id) no longer works after upgrading
Cause 1 of 3
The where value is undefined, so the condition is skipped
1Check
The 1.0 upgrade guide says null and undefined values in where were previously ignored silently. An ignored condition leaves the query with no filter.
grep -rn "findOneBy\|findOne(" src | head -202Fix
Check the value before querying, and treat a missing id as an error.
if (id === undefined || id === null) {
throw new Error("id is required")
}
const user = await userRepo.findOneBy({ id })3Verify
A request without an id now fails before the database is touched.
curl -i http://127.0.0.1:3000/users/Confirmed by typeorm.io
Cause 2 of 3
Code written for the old findOne(id) or findOne() form
1Check
The 0.3.0 release notes say the findOne signature with no parameters and the findOne(id) signature were dropped, and point to findOneBy for the id case.
grep -rn "findOne(" src2Fix
Rewrite those calls with an explicit where condition.
// before
// const user = await repo.findOne(id)
const user = await repo.findOneBy({ id })3Verify
The call returns the row with that id, or null when none exists.
console.log(await repo.findOneBy({ id: 1 }))Confirmed by github.com
Cause 3 of 3
You upgraded to 1.0 and the same code now throws
1Check
On 1.0 this is the new default, set by invalidWhereValuesBehavior. The throw is the guard working.
grep -rn "invalidWhereValuesBehavior" src2Fix
Fix the caller so it never passes undefined. Only set the option to ignore as a short bridge while you clean up.
new DataSource({
// ...
invalidWhereValuesBehavior: { null: "ignore", undefined: "ignore" },
})3Verify
Remove the bridge option once the callers are fixed and run the tests.
npm testConfirmed by typeorm.io
How do I stop it coming back?
- Validate ids at the controller or DTO layer so undefined never reaches a repository.
- Write a test that calls each lookup with a missing id and expects a failure.
- Run on 1.0 behavior in staging before you upgrade production.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
How to match NULL columns in TypeORM find options
A find call with a null value in where throws a TypeORMError
Write IsNull() from typeorm in the where object, not null.
2 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack end
NestJS GraphQL cannot work out the output type of a field or query
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.
In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.
2 causes, each with a fixBack end
IntelliJ IDEA breakpoints are ignored in a NestJS app
No 'Debugger listening' line appears when the NestJS app starts
IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fix