Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
a month ago

How can the error with the TypeORM generic findByOne and FindOptionsWhere be resolved?

In my Nestjs project using TypeORM, I have created the abstract class BaseRepository, which implements the BaseRepositoryInterface. Here's what the BaseRepositoryInterface looks like:
import { DeepPartial, FindManyOptions } from "typeorm";

export interface BaseRepositoryInterface<T> {
    create(data: DeepPartial<T>): T
    findOneById(id: number): Promise<T>
    findAll(options?: FindManyOptions<T>): Promise<T[]>
}
And here's the BaseRepository class:
import { DeepPartial, FindManyOptions, FindOptionsWhere, Repository } from "typeorm";
import { BaseRepositoryInterface } from "./base.interface.repository";

export abstract class BaseRepository<T> implements BaseRepositoryInterface<T> {
    private entity: Repository<T>;

    protected constructor(entity: Repository<T>) {
        this.entity = entity;
    }

    public create(data: DeepPartial<T>): T {
        return this.entity.create(data)
    }

    public async findOneById(id: number): Promise<T> {
        const options: FindOptionsWhere<T> = { id: id };
        return await this.entity.findOne(options);
    }

    public async findAll(options?: FindManyOptions<T>): Promise<T[]> {
        return await this.entity.find(options)
    }
}
However, I encountered an error in the findOneById method. The error message is shown below: enter image description here

I noticed this issue has been reported on the typeorm github page, but it has not yet been resolved. Can you provide any insights on how to fix this issue?
Answers(2)
avatar
Vogi
a month ago
Verified Answer
The error message you are seeing is due to the fact that the findOne method is not working as expected. One solution is to query by the _id field if you have it, or create a custom field to query by. Here's an example of how you can create a custom field to query by:
@Entity()
export class UserEntity {
    @ObjectIdColumn()
    _id: string;

    @PrimaryColumn()
    id: string;

    @Column()
    public name: string;
}
This way you can do a query like this:
await manager.findOne(User, { _id: <your _id>});
You can find more information about this issue on the Stack Overflow page.

Sources

Why is FindOne where id using typeorm and mongodb doesn't work?db.collection.findOne() — MongoDB ManualfindOneById without relations option does not return promise for ...node.js - Typescript TypeORM findOneBy({id: id}) fails in generic ...TypeORM- findOne returns unexpected value - Stack Overflow
avatar
Tolerim
a month ago
The error message is "Property 'findOneBy' does not exist on type 'Repository'. Did you mean 'findOne'?". The issue is that findOneBy method does not exist on the Repository class in TypeORM. Instead, you can use the findOne method and provide the id as the first argument. Here is how you can fix the findOneById method:
public async findOneById(id: number): Promise<T> {
  return await this.entity.findOne(id);
}
You also need to update the import statement for FindOptionsWhere to FindOneOptions since FindOptionsWhere does not exist in TypeORM.
import { DeepPartial, FindManyOptions, FindOneOptions, Repository } from "typeorm";
;