带有 nestjs 的 typeORM : return 类型的查找方法
typeORM with nestjs : return type of find method
我正在使用:nestJS、typescript、typeORM、postgresQL
我想检查输入的密码是否与数据库中的数据相同
这就是我定义用户实体的方式。
// User.entity.ts
import {Entity, PrimaryColumn, Column} from "typeorm";
@Entity()
export class User {
@PrimaryColumn({unique : true})
email: string;
@Column()
password: string;
@Column({unique : true})
nickname: string;
}
与find方法无关
async signIn(email : string, nickname : string, password : string) {
const repo = getRepository('User');
const user = await repo.findOne(email);
console.log(user);
}
> User { email: 'wasabi@mail.com', password: 'pass', nickname: 'wasabi' }
然后我尝试了这些方法,但失败了。
- 结果:
Error : Property "password" does not exist on type "unknown"
// *.service.ts
import {User} from '*/User.entity.ts'
async signIn(email : string, pass : string) {
const repo = getRepository('User');
const user = await repo.findOne(email);
if (user.password = pass)
console.log("right");
else
console.log("wrong");
}
- 结果:
Error : Type '{}' is missing the following properties from type 'Promise<User>': then, catch, finally, [Symbol.toStringTag]
// *.service.ts
import {User} from '*/User.entity.ts'
async signIn(email : string, password : string) {
const repo = getRepository('User');
const user : Promise<User> = await repo.findOne(email);
if (user.password = pass)
console.log("right");
else
console.log("wrong");
}
发生这种情况是因为 getRepository('User')
无法推断此存储库指的是哪个实体。请改用 getRepository(User)
然后您会看到 repo
是 Repository<User>
,而不是 Repository<unknown>
或者,您可以使用 User
作为类型:getRepository<User>('User')
如果您不想导入 class User
.
顺便说一句,这是 TypeScript 的东西。详细了解 Generics Types
我正在使用:nestJS、typescript、typeORM、postgresQL
我想检查输入的密码是否与数据库中的数据相同
这就是我定义用户实体的方式。
// User.entity.ts
import {Entity, PrimaryColumn, Column} from "typeorm";
@Entity()
export class User {
@PrimaryColumn({unique : true})
email: string;
@Column()
password: string;
@Column({unique : true})
nickname: string;
}
与find方法无关
async signIn(email : string, nickname : string, password : string) {
const repo = getRepository('User');
const user = await repo.findOne(email);
console.log(user);
}
> User { email: 'wasabi@mail.com', password: 'pass', nickname: 'wasabi' }
然后我尝试了这些方法,但失败了。
- 结果:
Error : Property "password" does not exist on type "unknown"
// *.service.ts
import {User} from '*/User.entity.ts'
async signIn(email : string, pass : string) {
const repo = getRepository('User');
const user = await repo.findOne(email);
if (user.password = pass)
console.log("right");
else
console.log("wrong");
}
- 结果:
Error : Type '{}' is missing the following properties from type 'Promise<User>': then, catch, finally, [Symbol.toStringTag]
// *.service.ts
import {User} from '*/User.entity.ts'
async signIn(email : string, password : string) {
const repo = getRepository('User');
const user : Promise<User> = await repo.findOne(email);
if (user.password = pass)
console.log("right");
else
console.log("wrong");
}
发生这种情况是因为 getRepository('User')
无法推断此存储库指的是哪个实体。请改用 getRepository(User)
然后您会看到 repo
是 Repository<User>
,而不是 Repository<unknown>
或者,您可以使用 User
作为类型:getRepository<User>('User')
如果您不想导入 class User
.
顺便说一句,这是 TypeScript 的东西。详细了解 Generics Types