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(); this.getHeroes();
} }
getHeroes(): void { async getHeroes(): Promise<void> {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes.slice(1, 5)); this.heroes = await this.heroService.getHeroes()
} }
} }

View File

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

View File

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

View File

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