0

I'm having problems displaying data from my local express.js REST API, which is structured like this:

 people: [{ surname: 'testsurname', name: 'testname', email: 
 '[email protected]', status: 1, activity: 'Office' }

I have a people service where I get the data, it looks like this:

export interface People {
  surname: String;
  name: String;
  email: String;
  status: Boolean;
  activity: String;
}

@Injectable()
export class PeopleService {

private _peopleURL = "http://localhost:8080/api/people";

constructor(private http: HttpClient) {
console.log('init PS')
}

getPeople(): Observable<People[]> {
return this.http
    .get(this._peopleURL)
    .map((response: Response) => {
        return <People[]>response.json();
    })
  }


}

This is my PeopleComponent.ts code

export class PeopleComponent implements OnInit {
  _peopleArray: People[];

  constructor(private ps: PeopleService)
   { }

   getPeople(): void {
    this.ps.getPeople()
        .subscribe(
            resultArray => this._peopleArray = resultArray,
            error => console.log("Error :: " + error)
        )
}


  ngOnInit(): void {
    this.getPeople();
  }

Now I'm trying to display the data (i.e. the name) in my 'People' component.html like so:

<div> {{people.name}} </div>

When I start my app I get an error saying

'TypeError: Cannot read property 'name' of undefined at Object.eval [as updateRenderer] 

Can anyone explain to me what I missed and what I need to be doing in order to display the data?

8
  • have you tried printing people[0].name ? as it is an array Commented Feb 8, 2018 at 8:39
  • @HrishikeshKale Yes, but then I'm getting "Cannot read property '0' of undefined" Commented Feb 8, 2018 at 8:46
  • If using HttpClient i would suggest: return this.http.get<People[]>(this._peopleURL , { observe: 'body', responseType: 'json'}); this way you don't need to .map() Commented Feb 8, 2018 at 8:46
  • _peopleArray.name will work Commented Feb 8, 2018 at 8:47
  • @HrishikeshKale Unfortunately not, still getting undefined Commented Feb 8, 2018 at 8:49

3 Answers 3

1

You response is json array .try following code snippet.

<div *ngFor ="let p of people">
      <div> {{p.name}} </div>
   </div>

Update

Change your service method as shown below.The default value that returns new HttpClient is Object. It automatically calls response.json() internally.

getPeople(): Observable<People[]> {
    return this.http
      .get<People[]>(this._peopleURL);

  }

Check out following working demo

WORKING DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Now I'm getting an error saying "Error :: TypeError: response.json is not a function"... any headers to what I did wrong?
Try to import 'rxjs/add/operator/map' That should fix the problem.
Thank you, this helps me plenty!
0

You don't even have "people" field in your component, you're setting it to "_peopleArray".

But even if you change it to

<div> {{_peopleArray.name}} </div>

it still won't work since _peopleArray is, well, an array.

You can iterate over array objects with *ngFor or access only first or nth element of the array like so

<div> {{_peopleArray[0].name}} </div>

3 Comments

Thank you! Now I'm getting an error saying "Error :: TypeError: response.json is not a function"... any headers to what I did wrong?
Can you console.log your response before trying to access it's data with .json()?
You shouldn't "return" anything from your Observable. Change your service method to this: " return this.http .get(this._peopleURL) .map((response: Response) => { <People[]>response.json(); }) } "
0

You should do this to print your JSON structure "Array of object"

   <div *ngFor ="let person of _peopleArray">
      <div> {{person.name}} </div>
   </div>

1 Comment

There's no need for safe navigation operator here :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.