I am encountering an issue with my User Entity's Email Column when using TypeORM's findOne function. Instead of returning null for a non-existent email, it is returning the first entry in the User Entity. This behavior does not align with the documentation provided.
findOne:
// returns the first User of database
const user = await this.userRepository.findOne({ email: 'example@email.com' });
User.Entity.ts:
import {
Entity,
Column,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn({ name: 'id' })
private _id: number;
@Column({ name: 'password', length: 256, nullable: true })
private _password?: string;
@Column({ name: 'email', length: 300, nullable: true, unique: true })
private _email?: string;
@Column({ name: 'roles', length: 300, nullable: true })
private _roles?: string = null;
public get id(): number {
return this._id;
}
public set id(id: number) {
this._id = id;
}
public get email(): string {
return this._email;
}
public set email(email: string) {
this._email = email;
}
public get password(): string {
return this._password;
}
public set password(password: string) {
this._password = password;
}
public get roles(): string {
return this._roles;
}
public set roles(roles: string) {
this._roles = roles;
}
}
The expected behavior according to the official documentation:
const user = new User(); user.firstName = "Timber"; user.lastName = "Saw"; user.age = 25; await repository.save(user);
const allUsers = await repository.find(); const firstUser = await repository.findOne(1); // find by id const timber = await repository.findOne({ firstName: "Timber", lastName: "Saw" });
await repository.remove(timber);