类型错误:无法读取未定义的属性(读取"问题记录") - 问题记录是棱镜模型表示



我正在尝试记录一个关于棱镜模型的问题。模型定义如下:

model QuestionRecord {
id                    String                  @id
institution           String
title                 String
imagepath             String
alternatives          String
subject               String
topic                 String
year                  Int
StudentQuestionRecord StudentQuestionRecord[]
}

要在该模型上做任何记录,我使用以下类及其类型:

import { randomUUID } from "crypto";
export interface QuestionEntity {
id: string,
year: number,
title: string,
topic: string,
subject: string,
imagepath: string,
institution: string,
alternatives: string,
};
export class Question {
public _id: string;
public props: Omit<QuestionEntity, 'id'>;

constructor(props: Omit<QuestionEntity, 'id'>, id?: string) {

this._id = id ?? randomUUID();
this.props = props;
}
public get id() {
return this._id;
}
public get title() {
return this.props.title;
}
public set title(newQuestionTitle) {
this.props.title = newQuestionTitle;
}
public get topic() {
return this.props.topic;
}
public set topic(newQuestionTopic) {
this.props.topic = newQuestionTopic;
}
public get subject() {
return this.props.subject;
}
public set subject(newQuestionSubject) {
this.props.subject = newQuestionSubject;
}
public get year() {
return this.props.year;
}
public set year(newYear) {
this.props.year = newYear
}
public get institution() {
return this.props.institution;
}
public set institution(newInstitution) {
this.props.institution = newInstitution;
}
public get alternatives() {
return this.props.alternatives;
}
public set alternatives(newAlternatives) {
this.props.alternatives = newAlternatives;
}
public get imagepath() {
return this.props.imagepath;
}
public set imagepath(newImage) {
this.props.imagepath = newImage;
}
}

最后,我有这个路由,允许请求创建一个问题:

@Post('create/question')
async createQuestionView(@Body() body: CreateQuestionBody) {
const {id, year, title, topic, subject, imagepath, institution, alternatives } = body;
const { question } = await this.createQuestion.execute({
id,
year,
title,
topic, 
subject,
imagepath,
institution,
alternatives 
});
return {question: QuestionViewModel.toHTTP(question)};
}

顺便说一下,下面是Shea建议的其他可能对故障排除有帮助的代码片段。下面是execute方法的编写方法:

interface CreateQuestionResponse {
question: Question;
}
@Injectable()
export class CreateQuestion {
constructor(private questionRepository: QuestionRepository) {}
async execute(request: QuestionEntity): Promise<CreateQuestionResponse> {
const {
institution, subject, topic, title,
year, alternatives, imagepath
} = request;
const question = new Question({
alternatives, 
institution, 
imagepath,
subject, 
topic, 
title,
year, 
});
await this.questionRepository.create(question)
return { question };
}
}

以下是PrismaQuestionRepository的写法:

export class PrismaQuestionRepository implements QuestionRepository {
constructor(private prisma: PrismaService) {}
async create(question: QuestionEntity): Promise<void> {
const raw = PrismaQuestionMappper.toPrisma(question);
await this.prisma.questionRecord.create({
data: raw,
})
}
async findQuestionById(questionId: string): Promise<QuestionEntity | null> {
const question = await this.prisma.questionRecord.findUnique({
where: {
id: questionId,
}
});
if (!question) return null;
return PrismaQuestionMappper.toDomain(question);
}
async findQuestionsByYear(year: number): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
year: year,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}
async findQuestionsByTitle(title: string): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
title: title,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}
async findQuestionsByTopic(topic: string): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
topic: topic,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}
async findQuestionsBySubject(subject: string): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
subject: subject,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}
async findQuestionsByInstitution(institution: string): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
institution: institution,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}
async findQuestionsBySubjectAndTopic(subject: string, topic: string): Promise<QuestionEntity[] | null> {
const questions = await this.prisma.questionRecord.findMany({
where: {
subject: subject,
topic: topic,
}
});
if (!questions) return null;
return questions.map(PrismaQuestionMappper.toDomain);
}   
}

mapper是这样写的:

export class PrismaQuestionMappper {
static toPrisma(question: QuestionEntity) {
return {
id: question.id,
year: question.year,
title: question.title,
topic: question.topic,
subject: question.subject,
imagepath: question.imagepath,
institution: question.institution,
alternatives: question.alternatives,
};
};
static toDomain(raw: QuestionEntity) {
return new Question({
year: raw.year,
title: raw.title,
topic: raw.topic,
subject: raw.subject,
imagepath: raw.imagepath,
institution: raw.institution,
alternatives: raw.alternatives,
});
};
};

我的应用程序运行得很好,直到我试图记录一个问题。每当我尝试,传入所有必要的字段,我得到这个错误:

[Nest] 13784  - 03/01/2023, 09:00:47   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'questionRecord')
TypeError: Cannot read properties of undefined (reading 'questionRecord')
at PrismaQuestionRepository.create (/home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/src/infra/database/prisma/repositories/prisma.question.repository.ts:12:27)
at CreateQuestion.execute (/home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/src/app/use-cases/question/create.question.ts:29:39)
at AppController.createQuestionView (/home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/src/infra/http/controllers/app.controller.ts:52:52)
at /home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at /home/famialiaaes/Documents/Estudos/NestJs/student-microservice/student/node_modules/@nestjs/core/router/router-proxy.js:9:17

我知道当一个未定义的事物的假定属性被调用时会发生这个错误。所以我重新阅读了我所有的代码,并将其重构为一些片段,同时我也对其进行了检查,以查看哪些代码可以被评估为未定义,但还不能淡化问题。我希望有人能帮助我,指出一些没有看到的细微差别。提前感谢!

问题解决:由于PrismaQuestionRepository是一个类,是nestjs模块的一部分,在它上面我应该放一个@Injectable装饰器,我从现在起就没有了。操作完成后,问题消失。

当您尝试运行this.prisma.questionRecord.create时,看起来this.prisma是未定义的。当实例化PrismaQuestionRepository时,必须传入prisma(查看其构造函数)。请确保您正确地实例化了PrismaQuestionRepository,这似乎是您错误的根源。

相关内容

  • 没有找到相关文章

最新更新