Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTOs in Angular

Tags:

angular

I'm making an angular project using asp.net for the back-end. So I want to make use of data transfer objects in angular but I'm having problems replicating the navigation properties. Is there anyway to do this?

My DTOs so far:

import { DocumentsDto } from './DocumentsDto';

export class CaseFilesDto {
    idCaseFile: number;
    CaseFileName: string;
    CreationDate: Date;
    CaseFileOwner: string;
    AmountOfDocuments: number;
    Document: DocumentsDto[];

    public CaseFilesDto() {
        this.Document = new DocumentsDto[];
    }
}

import { CaseFilesDto } from './CaseFilesDto';

export class DocumentsDto {
    idDocument: number;
    idCaseFile: number;
    DocumentName: string;
    CreationDate: Date;

    CaseFile: CaseFilesDto;
}

The problem here is in the line where it puts:

this.Document = new DocumentsDto[];

Using tslint on visual studio code I get that the error is that documentsDto misses the include property. But I can't find such thing.

Thank you!

Regards.

like image 733
Alex Varela Avatar asked Mar 02 '26 01:03

Alex Varela


1 Answers

You need to change the line to

this.Document = [];

In javascript and typescript you can create a new array just like that. You could even simplify it a bit like this.

export class CaseFilesDto {
    idCaseFile: number;
    CaseFileName: string;
    CreationDate: Date;
    CaseFileOwner: string;
    AmountOfDocuments: number;
    // Initialize the value
    Document: DocumentsDto[] = [];
}
like image 196
tom van green Avatar answered Mar 04 '26 16:03

tom van green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!