Warm tip: This article is reproduced from serverfault.com, please click

Angular 11 HttpClient Generic Observable map service values into a Class

发布于 2020-11-28 03:39:54

I'm consuming a POST service in .net that returns the next structure, the service return data correctly

enter image description here

public sendExcelInBase64ToBackEnd(data: any) {
    this.customOptions = {
        headers: this.customHttp.buildHeader(),
    };

    this.customHttp.Post<SerialResultDTO>('/serialsExcel', data, this.customOptions).subscribe(val => { // <- val data returns ok
        this.serialResult = val;
        let x = this.serialResult.Success;
        console.log(x);
        console.log(val.Success); // <- prints undefined
    })
    
    return null;
}

When I try print in console 'val.Success' it prints undefined!!, Why this happens??

I want assign all atributes to a custom class in angular, but in chrome debug it shows object data, but in time of asign to my model class it gets undefined!!

Someone can help me please!! I appreciate that!!

Questioner
Chris. R.
Viewed
0
MoxxiManagarm 2020-11-28 13:07:46

Your image shows a lowercase success, but you try to log a titlecase Success. Change to

public sendExcelInBase64ToBackEnd(data: any) {
    this.customOptions = {
        headers: this.customHttp.buildHeader(),
    };

    this.customHttp.Post<SerialResultDTO>('/serialsExcel', data, this.customOptions).subscribe(val => {
        this.serialResult = val;
        let x = this.serialResult.success;
        console.log(x);
        console.log(val.success);
    })
    
    return null;
}