首页 > 解决方案 > Nest js + prisma项目,服务中findUnique方法出错

问题描述

当使用带有嵌套的棱镜时,我有疑问我做错了什么。我收到此错误

src/modules/auth/auth.service.ts:28:63 - error TS2322: Type 'UserWhereUniqueInput' is not assignable to type 'string'.
28     const user = await this.prisma.user.findUnique({ where: { email } });
                                                                 ~~~~~

  node_modules/.prisma/client/index.d.ts:1521:5
    1521     email?: string
             ~~~~~
    The expected type comes from property 'email' which is declared here on type 'UserWhereUniqueInput'
[11:50:56 PM] Found 1 error. Watching for file changes

棱镜误差

在 auth.service.ts

...
@Injectable()
export class AuthService {
  constructor(private jwtService: JwtService, private prisma: PrismaService) {}

  async signIn({
    email,
    password,
  }: {
    email: Prisma.UserWhereUniqueInput;
    password: string;
  }) {
    const user = await this.prisma.user.findUnique({ where: { email } });
...

我的用户模式是下一个

model User {
 id         Int      @id @default(autoincrement())
 email      String   @unique
 password   String
 lastName   String?
 firstName  String?
 roles      String[]
}

正在调用服务的控制器是

...
@Post('sign-in')
  signIn(@Body() signinAuthDto: any) {
    return this.authService.signIn(signinAuthDto);
  }

我在这里添加了 any 而不是 SigninAuthDto 但它仍然失败

export class SigninAuthDto {
  email: string;
  password: string;
}

标签: typescriptnestjsprisma

解决方案


为什么不在方法 signIn 中将属性电子邮件的类型设为字符串?问题是您正在尝试键入作为字符串的电子邮件到对象,prisma 唯一输入。

@Injectable()
export class AuthService {
  constructor(private jwtService: JwtService, private prisma: PrismaService) {}

  async signIn({
    email,
    password,
  }: {
    email: string;
    password: string;
  }) {
    const user = await this.prisma.user.findUnique({ where: { email } });

希望这会有所帮助!


推荐阅读