0

right now i'm playing with ASP.NET with Angular 6 in frontend and can't figure out what is happening. For short: i'm getting a empty json although backend is sending right values:

checked with Postman

Model:

import { Car } from '../models/car';
import { Ad } from '../models/ad';

export class Owner {
    Id: number;
    Firstname: string;
    Lastname: string;
    Phone: number;
    Email: string;
    Cars: Car[];
    Ads: Ad[];
  }

Service:

      /** GET owners list */
  getOwners (): Observable<Owner[]> {
    return this.http.get<Owner[]>(this.ownersUrl)
    .pipe(
      catchError(this.handleError('getOwners', []))
    );
  }

Owners Component:

  getOwners(): void{
this.ownerService.getOwners()
    .subscribe( owners => this.owners = owners);  
}

Owners Html:

<nb-card class="nav-card">
    <nb-card-body>
        <nb-list>
            <nb-list-item *ngFor="let owner of owners" >
                {{owner.Id}}
            </nb-list-item>
        </nb-list>
    </nb-card-body>
</nb-card>

What am i doing wroing? Service constantly giving me empty json body isn't nice :/

Lacwik
  • 1
  • 1
  • you are not setting headers in your get request, but set in postman. remove the catchError pipe and see what errors you get. – Fateme Fazli Dec 15 '18 at 10:05
  • Your `Owner` class has different variables and response has different variables. So, that's why it is not able to map. For example, Owner class has `Id` but response has `id`. – Saddam Pojee Dec 15 '18 at 10:32

1 Answers1

0

as I see it properly, how did you check that the Service returns you empty json body? Please, check the following:

  1. Your Owner class has capitalized fields, but, regarding the Postman screenshot, your server sends all lowercase fields.
  2. In Postman you send some headers, so, please, check that the data may be requested without these headers, or set these headers in your Service:

     getOwners(): Observable<Owner[]> {
    
        const httpOptions = {
          headers: new HttpHeaders({
            'Header Key':  'Header Value',
          })
        };
    
        return this.http.get<Owner[]>(this.ownersUrl)
          .pipe(
            catchError(this.handleError('getOwners', []))
          );
    }