简体   繁体   English

错误 TS2740 类型 'DeepPartial<quiz> []' 缺少类型“问题”的以下属性:id、问题、hasId、save 和另外 4 个</quiz>

[英]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.

相关问题 TS2740 缺少 type、Typescript、NestJS 中的以下属性 - TS2740 is missing the following properties from type, Typescript, NestJS 类型“Element”缺少类型“OfferProps”中的以下属性:id、firstName、city、price 和另外 2 个。 TS2740 - Type 'Element' is missing the following properties from type 'OfferProps': id, firstName, city, price, and 2 more. TS2740 TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性 - TS error: Type 'event' is missing the following properties from type 'keyboardevent' 错误 TS2739:类型“{}”缺少类型中的以下属性 - error TS2739: Type '{}' is missing the following properties from type 类型 &#39;Object&#39; 缺少类型 &#39;any[]&#39; 的以下属性:length、pop、push、concat 和 26 more.ts - Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts 类型“{}”缺少类型 ts(2739) 中的以下属性 - Type '{}' is missing the following properties from type ts(2739) 错误:类型“{}”缺少类型中的以下属性 - Error: Type '{}' is missing the following properties from type 类型“{}”缺少类型“IResponseConfig”中的以下属性 - Type '{}' is missing the following properties from type 'IResponseConfig' TS2739 类型“any[]”缺少来自“JSON”类型的以下属性:解析、字符串化、[Symbol.toStringTag] - TS2739 Type 'any[]' is missing the following properties from type 'JSON': parse, stringify, [Symbol.toStringTag] 类型“文档”缺少类型中的以下属性 - Type 'Document' is missing the following properties from type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM