0

I have the following typescript TypeORM model:

@Entity()
export class Teacher extends EntityBase {
    @Column({ length: 256 })
    public firstName: string

    @Column({ length: 256 })
    public lastName: string

    @Column({ unique: true, length: 256 })
    public email: string

    @ManyToMany((type) => Student, (student) => student.teachers, { nullable: true })
    @JoinTable()
    public students: Student[]

    constructor(first: string, last: string, email: string) {
        super();
        this.firstName = first;
        this.lastName = last;
        this.email = email;
        //this.students = []; TypeORM initialization failed! InitializedRelationError: Array initializations are not allowed in entity relations
    }
}

The following query:

    public override async GetByEmail (email: string): Promise<Teacher | null> {
        return await this._repository.findOneOrFail({
            where: { email: email },
            relations: ["students"]
        });
    }

failed:

Exception: {"criteria":{"where":{"email":"[email protected]"},"relations":["students"]},"message":"Could not find any entity of type \"Teacher\" matching: {\n    \"where\": {\n        \"email\": \"[email protected]\"\n    },\n    \"relations\": [\n        \"students\"\n    ]\n}"}

1 Answer 1

0

findOneOrFail will throw. Need to catch exception and handle it as non-existing entity. I switched to findOne

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.