Nico Schuck
Nico Schuck

Reputation: 972

Angular httpClientModule

I can´t handle res in array form by httpClientModule. The Api is created by NodeJS. A easy try looks like that:

res.send( [ { message: 'lego api!' } , { name: 'lucy' } ] );

The response works when i test it by my browser. No i wants to use the same url in Angular. For I imported the httpClientModule in app.module. The app component looks like that:

import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

  ngOnInit(): void {
    console.log(this.list());
  }
  list(): Observable<any> {
    return this.http.get('http://localhost:8080/api').map(data => data.message);
  }

In console i only get the informations of the observable:

Observable {_isScalar: false, source: Observable, operator: MapOperator} operator : MapOperator {project: ƒ, thisArg: undefined}

source : Observable {_isScalar: false, source: Observable, operator: MapOperator}

_isScalar : false

proto : Object

I dont get what i´m doing wrong.

Upvotes: 0

Views: 1034

Answers (1)

theykillimmortal
theykillimmortal

Reputation: 149

You need to subscribe to your Observable.

An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method.

docs

Upvotes: 2

Related Questions