[英]How to solve this error (Nest & Prisma): ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'organ')
I'm developing an API using Nestjs and Prisma, the problem occurs when I try to insert information in the Database (Postgres), I get this error, saying that it cannot read the "organ" property which is the table in which I intend to insert the information.我正在使用 Nestjs 和 Prisma 开发一个 API,当我尝试在数据库 (Postgres) 中插入信息时出现问题,我得到这个错误,说它无法读取我想要的表的“organ”属性插入信息。
The error is occurring in the repository in the create method: `错误发生在存储库中的创建方法中:`
import { Organ } from '@prisma/client';
import { CreateOrganDto } from '../../../../../modules/organs/dto/create-organ.dto';
import { IOrganRepository } from '../../../../../modules/organs/repositories/IOrganRepository';
import { PrismaService } from '../../../../../shared/infra/db/prisma.service';
export class OrganRepository implements IOrganRepository {
constructor(private prisma: PrismaService) {}
async create({ name }: CreateOrganDto): Promise<Organ> {
const organ = await this.prisma.organ.create({
data: {
name
},
});
return organ;
}
async findByName(name: string): Promise<Organ | null> {
const organ = await this.prisma.organ.findUnique({
where: {
name,
},
});
return organ;
}
}
` `
a const organ dev const 器官开发者
this.prisma.organ.create method is returning an Organ, never type when it should just return Organ
It looks like the PrismaClient is not in sync with Prisma Schema.看起来 PrismaClient 与 Prisma Schema 不同步。 You would need to invoke
npx prisma generate
in order to regenerate the PrismaClient - Reference .您需要调用 npx
npx prisma generate
才能重新生成 PrismaClient- Reference 。
Also, I would recommend checking that all models defined in schema file has been created in the database, you can either use npx prisma migrate dev
or npx prisma db push
to create the corresponding tables - Reference .此外,我建议检查模式文件中定义的所有模型是否已在数据库中创建,您可以使用
npx prisma migrate dev
或npx prisma db push
创建相应的表 - Reference 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.