Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript Nest js+;prisma项目,服务中findUnique方法错误_Typescript_Nestjs_Prisma - Fatal编程技术网

Typescript Nest js+;prisma项目,服务中findUnique方法错误

Typescript Nest js+;prisma项目,服务中findUnique方法错误,typescript,nestjs,prisma,Typescript,Nestjs,Prisma,我有一个问题,当使用prisma和nest时,我做错了什么。 我得到了这个错误 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 } });

我有一个问题,当使用prisma和nest时,我做错了什么。 我得到了这个错误

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);
  }
我在这里添加了任何,而不是 这很重要,但还是失败了

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

为什么不在方法signIn just string中创建属性email的类型?问题是,您正试图将字符串形式的电子邮件输入到对象prisma unique input

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

希望这有帮助

如果我将类型添加到服务参数中,那么它将因错误而崩溃。TS2345:类型为“{email:string;}”的参数不能分配给类型为“{select?:UserSelect;rejectOnNotFound?:rejectOnNotFound;其中:UserWhereUniqueInput;}”的参数。
对象文字只能指定已知属性,并且类型中不存在“email”{select?:UserSelect;rejectOnNotFound?:rejectOnNotFound;where:USERWHERUNIQUEINT;}。只有当我删除键入参数时,它才能创建不带类型的用户,错误是下一个```类型的参数{email:any;密码:string;}'不能分配给类型为{select?:UserSelect;数据:(没有UserCreateInput,UserUncheckedCreateInput&;UserUncheckedCreateInput)|(没有…&;UserCreateInput);}.
对象文字只能指定已知的属性,并且类型{select?:UserSelect;数据中不存在“电子邮件”:(没有UserUncheckedCreateInput,UserUncheckedCreateInput&;UserUncheckedCreateInput)|(没有…&;UserCreateInput);}.“``@VictorOrlyk您描述了两个不同的错误。我在本地尝试并键入了
电子邮件
,因为
string
findUnique
一起工作时没有错误。您能否更新您的问题,以包括您如何尝试创建用户?