Estou tentando carregar uma lista em tela no meu navegador usando Observable, porém não estou tendo sucesso, esse é registro que existe na minha url;
Essa é a classe de serviços
import { Http } from '@angular/http';
import { environment } from './../../environments/environment';
import { Restaurant } from './restaurant.model';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class RestaurantService {
public url: String = 'http://localhost:3000/api/';
constructor(private http: Http) {
// this.url = environment.url;
}
restaurants(): Observable<Restaurant[]> {
return this.http.get(`${this.url}/restaurants`)
.map(response => response.json())
}
realizei um teste na classe de componente utilizando console.log como podem ver abaixo;
export class RestaurantsComponent implements OnInit {
restaurants: Restaurant[]
constructor(private restaurantService: RestaurantService){}
ngOnInit() {
this.restaurantService.restaurants()
.subscribe(restaurants => {
console.log('o valor é ' , this.restaurants = this.restaurants);
})
}
E o resultado está sendo esse;
o valor é undefined
O que será que estou fazendo de errado?