typescript - 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;
}
解决方案
为什么不在方法 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 } });
希望这会有所帮助!
推荐阅读
- c# - 在实体的 IQueryable 中使用相关实体的 IQueryable
- html - Angular生成刷新按钮单击
- python - 如果列是日期,熊猫,则按列名对数据框进行排序?
- html - 在 HTML 中生成的客户端中的 Swagger-Editor 空响应
- angular - 对 NGXS 表单 UpdateFormValue 执行副作用
- python-3.x - 需要建议阅读特定框架
- javascript - 重新映射 json 对象并将它们推送到一个数组中
- javascript - 如何隐藏表格行中的值并在单击加号时显示它
- business-objects - 没有数据时如何显示列
- elm - 如何使用 Elm 在多个组件之间共享存储?