gpt4 book ai didi

typescript - TypeORM- findOne 返回意外值

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:38 26 4
gpt4 key购买 nike

我有一个带有电子邮件列的用户实体。当我使用 TypeORM 的 findOne 函数搜索数据库中不存在的电子邮件时,findOne 出于某种原因将第一个条目返回给用户实体。此功能似乎不像文档中那样工作。

findOne:

// returns the first User of database
const user = await this.userRepository.findOne({ email: 'this@mailisnotindatabase.de' });

用户.实体.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;
}
}

这是来自官方文档:

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);

最佳答案

对我有用:

const user = await this.userRepository.findOne(
{ where:
{ email: 'this@mailisnotindatabase.de' }
}
);

关于typescript - TypeORM- findOne 返回意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com