now it works, it was CORS

This commit is contained in:
2021-01-14 17:00:56 +01:00
parent 989e617ffe
commit 8374e1831b
4 changed files with 17 additions and 16 deletions

View File

@ -16,8 +16,8 @@ export class DashboardComponent implements OnInit {
this.getHeroes();
}
getHeroes(): void {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes.slice(1, 5));
async getHeroes(): Promise<void> {
this.heroes = await this.heroService.getHeroes()
}
}

View File

@ -20,9 +20,9 @@ export class HeroDetailComponent implements OnInit {
this.getHero();
}
getHero(): void {
async getHero(): Promise<void> {
const id = +this.route.snapshot.paramMap.get('id');
this.heroService.getHero(id).subscribe(hero => this.hero = hero);
this.hero = await this.heroService.getHero(id)
}
goBack(): void {
@ -30,6 +30,6 @@ export class HeroDetailComponent implements OnInit {
}
save(): void {
this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
// this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
}
}

View File

@ -1,10 +1,9 @@
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Hero } from './hero';
import { MessageService } from './message.service';
import { HEROES } from './mock-heroes'
@Injectable({
providedIn: 'root'
@ -12,16 +11,14 @@ import { HEROES } from './mock-heroes'
export class HeroService {
constructor(private messageService: MessageService, private http: HttpClient) { }
getHeroes(): Observable<Hero[]> {
getHeroes(): Promise<Hero[]> {
this.messageService.add('HeroService: fetched heroes');
// return of(HEROES)
return this.http.get<Hero[]>(`http://172.16.3.185:5000/heroes`)
return this.http.get<Hero[]>(`http://172.16.3.185:5000/heroes`).toPromise()
}
getHero(id: number): Observable<any> {
getHero(id: number): Promise<Hero> {
this.messageService.add(`HeroService: fetch hero id=${id}`);
// return of(HEROES.find(hero => hero.id === id))
return this.http.get<Hero>(`http://172.16.3.185:5000/hero?id=${id}`)
return this.http.get<Hero>(`http://172.16.3.185:5000/hero?id=${id}`).toPromise()
}
updateHero(hero: Hero): Observable<any> {

View File

@ -18,8 +18,12 @@ export class HeroesComponent implements OnInit {
constructor(private heroService: HeroService, private messageService: MessageService) { }
getHeroes(): void {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes);
async getHeroes() {
try {
this.heroes = await this.heroService.getHeroes();
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
ngOnInit(): void {