'How to accept multiple objects into an array NestJS

I have a feedbackQuestion schema which takes (title: string, subtitle: string, types: enum, values: enum)

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document } from 'mongoose'
import { Types, Value } from 'src/common/enum/types.enum'

export type FeedbackQuestionDocument = FeedbackQuestion & Document

@Schema({ timestamps: true, id: true })
export class FeedbackQuestion {
    @Prop()
    title: string

    @Prop()
    subtitle: string

    @Prop()
    types: Types

    @Prop()
    value: Value
}

export const FeedbackQuestionSchema =
    SchemaFactory.createForClass(FeedbackQuestion)

The feedbackQuestion schema serves as a subdocument in my feedback schema for the key question

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import mongoose, { Document, ObjectId } from 'mongoose'
import { User } from './user.schema'
import { Transform, Type } from 'class-transformer'
import { FeedbackQuestion } from './feedback-question.schema'

import { distributionChannels } from 'src/common/enum/distributionChannels.enum'

export type FeedbackDocument = Feedback & Document

@Schema({ timestamps: true, id: true })
export class Feedback {
    @Transform(({ value }) => value.toString())
    _id: ObjectId

    @Prop()
    label: string

    @Prop({ default: false })
    status: boolean

    @Prop()
    question: [FeedbackQuestion]

    @Prop()
    comment: string

    @Prop()
    thankYouMessage: string

    @Prop()
    distributionChannels: distributionChannels

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
    @Type(() => User)
    user: User
}

export const FeedbackSchema = SchemaFactory.createForClass(Feedback)

when creating my create-feedbackDto, I assigned question to be an array of type feedbackQuestion

import { Type } from 'class-transformer'
import { FeedbackQuestion } from '../../schemas/feedback-question.schema'
import { IsArray, IsEnum, IsNotEmpty, ValidateNested } from 'class-validator'
import { Types, Value } from '../enum/types.enum'
import { distributionChannels } from '../enum/distributionChannels.enum'

export class CreateFeedbackDto {
    @IsNotEmpty()
    label: string

    status: boolean

    @IsArray()
    @ValidateNested({ each: true })
    @Type(() => FeedbackQuestion)
    question: FeedbackQuestion[]

    @IsNotEmpty()
    comment: string

    @IsNotEmpty()
    thankYouMessage: string

    @IsNotEmpty()
    title: string

    @IsNotEmpty()
    subtitle: string

    @IsEnum(Types)
    @IsNotEmpty()
    types: Types

    @IsEnum(Value)
    @IsNotEmpty()
    value: Value

    @IsEnum(distributionChannels)
    @IsNotEmpty()
    distributionChannels: distributionChannels
}

In my feedback services, I want to work on questions such that I can pass in multiple objects of feedbackQuestion into the question array when creating a feedback. Please How can I do that? The current code only takes one FeedbackQuestion object in the array

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { Feedback, FeedbackDocument } from '../../schemas/feedback.schema'
import {
    FeedbackQuestion,
    FeedbackQuestionDocument,
} from '../../schemas/feedback-question.schema'
import { IServiceResponse } from '../../common/interfaces/service.interface'
import { CreateFeedbackDto } from 'src/common/dto/create-feedback.dto'

@Inject#5406able()
export class FeedbackService {
    constructor(
        @Inject#5406Model(Feedback.name)
        private feedbackDocumentModel: Model<FeedbackDocument>,
        @Inject#5406Model(FeedbackQuestion.name)
        private feedbackQuestionDocumentModel: Model<FeedbackQuestionDocument>,
    ) {}

    async createFeedback(payload: CreateFeedbackDto): Promise<IServiceResponse> {
        const feedbackQuestion = await this.feedbackQuestionDocumentModel.create({
            title: payload.title,
            subtitle: payload.subtitle,
            type: payload.types,
            value: payload.value,
        })
        const feedback = await this.feedbackDocumentModel.create({
            label: payload.label,
            status: payload.status,
            question: [feedbackQuestion],
            comment: payload.comment,
            thankYouMesage: payload.thankYouMessage,
            distributionChannels: payload.distributionChannels,
        })

        // feedback.question.push(feedbackQuestion)

        await feedback.save()

        return {
            data: {
                user: feedback,
            },
        }
    }
}

This is the current response I get

 "label": "Yearly Feedback",
            "status": false,
            "question": [
                {
                    "title": "Yearly Feedback",
                    "subtitle": "Rating the Yearly performance of the organization",
                    "value": 1,
                    "_id": "627fa9b915d31bbbc0fe6908",
                    "createdAt": "2022-05-14T13:08:09.180Z",
                    "updatedAt": "2022-05-14T13:08:09.180Z",
                    "__v": 0
                }
            ],

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source