I'm struggling to resolve this error. Can anyone provide guidance on what needs to be corrected in order for this code to function properly?
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);
}
}
The above code snippet triggers the following error message:
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.
15 return await this.questionRepository.save(question); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/modules/quiz/question.service.ts:15:47 - error TS2769: No overload matches this call. Overload 1 of 4, '(entities: DeepPartial[], options?: SaveOptions): Promise<(DeepPartial & Quiz)[]>', gave the following error. Argument of type 'CreateQuestionDto' is not assignable to parameter of type 'DeepPartial[]'. Type 'CreateQuestionDto' is missing the following properties from type 'DeepPartial[]': length, pop, push, concat, and 29 more. Overload 2 of 4, '(entity: DeepPartial, options?: SaveOptions): Promise<DeepPartial & Quiz>', gave the following error. Type 'CreateQuestionDto' has no properties in common with type 'DeepPartial'.
15 return await this.questionRepository.save(question);
Description of the Question entity:
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('questions')
export class Question extends BaseEntity {
@PrimaryGeneratedColumn()
id: string;
@Column({
type: 'varchar',
})
question: string;
}
Details about the Question repository:
import { EntityRepository, Repository } from 'typeorm';
import { Quiz } from './quiz.entity';
@EntityRepository(Quiz)
export class QuestionRepository extends Repository<Quiz> {}
CreateQuestion dto details:
import { IsNotEmpty, Length } from 'class-validator';
export class CreateQuestionDto {
@IsNotEmpty()
@Length(3, 255)
question: string;
}
Question repository information:
import { EntityRepository, Repository } from 'typeorm';
import { Quiz } from './quiz.entity';
@EntityRepository(Quiz)
export class QuestionRepository extends Repository<Quiz> {}