Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create dto of an complex object in NestJs

I’m a beginner at NestJs and I want to create a dto of the following struct:

I want to create an API that can return this object using DTOs.

export let Week = [
    {
        DayName : "TuesDay",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    },
    {
        DayName : "Wednesday",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    },
    {
        DayName : "Friday",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    }
]

I tried the following dto but I got an error in the service provider :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I create 3 classes OneSessionResponseDto, SessionsResponseDto, and WeekResponseDto so I can use them as a dto of the object above.

export class OneSessionResponseDto {
    start: number;
    end: number;
    isReserved: boolean;
    reserver: string;
}

export class SessionsResponseDto {
    First: OneSessionResponseDto;
    Second: OneSessionResponseDto;
    Third: OneSessionResponseDto;
}

export class WeekResponseDto {
    DayName: string;
    TimeZone: SessionsResponseDto;
}

this is the service provider class:

@Injectable()
export class AppService {
    private week = Week;
    getWeek() : WeekResponseDto[] {
        return (this.week); <= the error
    }
}

I got this error here:

is missing the following properties from type ‘SessionsResponseDto’: First, Second, Third

>Solution :

You almost have it. You need to set TimeZone‘s type to SessionsResponseDto[] because it’s an array. Then change OneSessionResponseDto#start to OneSessionResponseDto#Start (the capitalization is important). You can see a working solution in the typoescript playground here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading