Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM findOne returns a row you did not ask for

ErrorfindOneBy({ id }) returns a user that is not the one requested

Short 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.

  • Applies to TypeORM 0.3.x, TypeORM 1.0, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 })
  2. 2Match what you see, then jump to its fix

    1. 1id prints undefined and the first row of the table comes backThe where value is undefined, so the condition is skippedFix
    2. 2Wrong or missing results after upgrading from 0.2 to 0.3Code written for the old findOne(id) or findOne() formFix
    3. 3TypeORMError about a null or undefined value in whereYou upgraded to 1.0 and the same code now throwsFix

What does the error look like?

  • findOneBy({ id }) returns a user that is not the one requested
  • findOne() 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 -20

2Fix

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

Did this fix it?

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(" src

2Fix

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

Did this fix it?

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" src

2Fix

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 test

Confirmed by typeorm.io

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.