[英]error TS2740 Type 'DeepPartial<Quiz>[]' is missing the following properties from type 'Question': id, question, hasId, save, and 4 more
I don't know how to fix this error.我不知道如何解决这个错误。 Does anyone know what I need to fix to get this code to work?
有谁知道我需要修复什么才能使此代码正常工作?
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateQuestionDto } from './dto/create-question.dto';
import { Question } from './question.entity';
import { QuestionRepository } from './question.repository';
@Injectable()
export class QuestionService {
constructor(
@InjectRepository(QuestionRepository)
private questionRepository: QuestionRepository,
) {}
async createQuestion(question: CreateQuestionDto): Promise<Question> {
return await this.questionRepository.save(question);
}
}
Returns the following error:返回以下错误:
src/modules/quiz/question.service.ts:15:5 - error TS2740: Type 'DeepPartial[]' is missing the following properties from type 'Question': id, question, hasId, save, and 4 more. src/modules/quiz/question.service.ts:15:5 - 错误 TS2740:“DeepPartial[]”类型缺少“问题”类型的以下属性:id、question、hasId、save 和另外 4 个。
15 return await this.questionRepository.save(question); 15 返回等待 this.questionRepository.save(question); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
src/modules/quiz/question.service.ts:15:47 - error TS2769: No overload matches this call. src/modules/quiz/question.service.ts:15:47 - 错误 TS2769:没有重载匹配此调用。 Overload 1 of 4, '(entities: DeepPartial[], options?: SaveOptions): Promise<(DeepPartial & Quiz)[]>', gave the following error.
Overload 1 of 4, '(entities: DeepPartial[], options?: SaveOptions): Promise<(DeepPartial & Quiz)[]>',给出了以下错误。 Argument of type 'CreateQuestionDto' is not assignable to parameter of type 'DeepPartial[]'.
“CreateQuestionDto”类型的参数不能分配给“DeepPartial[]”类型的参数。 Type 'CreateQuestionDto' is missing the following properties from type 'DeepPartial[]': length, pop, push, concat, and 29 more.
类型“CreateQuestionDto”缺少类型“DeepPartial[]”中的以下属性:长度、弹出、推送、连接等 29 个。 Overload 2 of 4, '(entity: DeepPartial, options?: SaveOptions): Promise<DeepPartial & Quiz>', gave the following error.
Overload 2 of 4, '(entity: DeepPartial, options?: SaveOptions): Promise<DeepPartial & Quiz>',给出了以下错误。 Type 'CreateQuestionDto' has no properties in common with type 'DeepPartial'.
“CreateQuestionDto”类型与“DeepPartial”类型没有共同的属性。
15 return await this.questionRepository.save(question); 15 返回等待 this.questionRepository.save(question);
Question entity:问题实体:
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('questions')
export class Question extends BaseEntity {
@PrimaryGeneratedColumn()
id: string;
@Column({
type: 'varchar',
})
question: string;
}
Question repository:问题库:
import { EntityRepository, Repository } from 'typeorm';
import { Quiz } from './quiz.entity';
@EntityRepository(Quiz)
export class QuestionRepository extends Repository<Quiz> {}
CreateQuestion dto: CreateQuestion dto:
import { IsNotEmpty, Length } from 'class-validator';
export class CreateQuestionDto {
@IsNotEmpty()
@Length(3, 255)
question: string;
}
Question repository:问题库:
import { EntityRepository, Repository } from 'typeorm';
import { Quiz } from './quiz.entity';
@EntityRepository(Quiz)
export class QuestionRepository extends Repository<Quiz> {}
The problem is that your QuestionRepository
is pointing to another entity ( Quiz
).问题是您的
QuestionRepository
指向另一个实体( Quiz
)。
Change it to:将其更改为:
export class QuestionRepository extends Repository<Question> {}
That way you can avoid the any
clause and use the dto directly on the save
call.这样,您可以避免使用
any
子句并直接在save
调用上使用 dto。
async createQuestion(question: CreateQuestionDto): Promise<Question> {
return await this.questionRepository.save(question);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.