[英]Node.JS Typescript TypeORM findOneBy({id: id}) error
I am using "typeorm": "^0.3.7"
.我正在使用
"typeorm": "^0.3.7"
。
public override async GetById (id: number): Promise<Student | null> {
return await this._repository.findOneByOrFail({ id: id });
}
fails:失败:
error TS2345: Argument of type '{ id: number; }' is not assignable to parameter of type 'FindOptionsWhere<Student> | FindOptionsWhere<Student>[]'.
EntityBase.ts
: EntityBase.ts
:
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
@Entity()
export abstract class EntityBase extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number
@Column()
public created: Date
@Column()
public modified: Date
constructor() {
super();
this.created = new Date();
}
}
Student.ts
entity: Student.ts
实体:
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
import { EntityBase } from "./EntityBase"
@Entity()
export class Student extends EntityBase {
@Column()
public firstName: string
@Column()
public lastName: string
@Column({ unique: true })
public email: string
@Column()
public isSuspended: Boolean
constructor(first: string, last: string, email: string, isSuspended?: boolean) {
super();
this.firstName = first;
this.lastName = last;
this.email = email;
this.isSuspended = isSuspended ?? false;
}
}
RepositoryBase.ts
: RepositoryBase.ts
:
import { IRepository, EntityBase } from "core"
import { Database } from "../../db"
import { EntityTarget, Repository } from "typeorm"
export abstract class RepositoryBase<T extends EntityBase> implements IRepository<T> {
protected _repository: Repository<T>;
constructor(entity: EntityTarget<T>) {
this._repository = Database.AppDataSource.getRepository(entity);
}
public async GetById (id: number): Promise<T | null> {
return await this._repository.findOneByOrFail({ id: id });
}
public async GetByEmail (email: string): Promise<T | null> {
return await this._repository.findOneByOrFail({ email: email });
}
public async ListAll (): Promise<T[]> {
return await this._repository.find();
}
public async Add (entity: T): Promise<T> {
return await this._repository.save(entity)
}
public async Update (entity: T): Promise<T> {
entity.modified = new Date();
return await this._repository.save(entity);
}
public async Delete (id: number): Promise<Number> {
let result = await this._repository.delete(id);
return result.affected ?? 0;
}
}
StudentRepository.ts
: StudentRepository.ts
:
import { EntityBase, Student } from "core"
import { RepositoryBase } from "./RepositoryBase"
import { FindOptionsWhere } from "typeorm"
export class StudentRepository<Student extends EntityBase> extends RepositoryBase<Student> {
constructor() {
super(Student);
}
}
What do I miss?我想念什么?
In the StudentRepository we don't need to add the generic type variables in <>.在 StudentRepository 中,我们不需要在 <> 中添加泛型类型变量。 Essentially you want to extend the RepositoryBase with Student type so this will work
本质上,您想使用 Student 类型扩展 RepositoryBase,这样就可以了
export class StudentRepository extends RepositoryBase<Student> {
constructor() {
super(Student);
}
public override async GetById (id: number): Promise<Student | null> {
return this._repository.findOneByOrFail({ id });
}
}
Writing StudentRepository<Student extends EntityBase>
makes it generic as well - it's the same as writing T (or anything else) rather than Student.编写
StudentRepository<Student extends EntityBase>
也使其具有通用性 - 这与编写 T(或其他任何内容)而不是 Student 相同。
Hope this solves it for you as well!希望这也能为您解决问题!
PS just a side note: await keyword isn't required when returning from an async function unless it's in a try..catch as explained here PS只是一个旁注:从异步function返回时不需要await关键字,除非它在try..catch中,如here所述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.