Simple reading async api request in angular app not working

Discussão em 'Angular' iniciado por Yardi, Fevereiro 11, 2025.

  1. Yardi

    Yardi Guest

    I just want to read data from my api, but I can't get to the structure, only observable or promise. My service:

    @Injectable({
    providedIn: 'root'
    })
    export class AttractionService {

    constructor(private http: HttpClient) { }

    async getAtttractions(): Promise<Observable<any>> {
    return this.http.get<any>("https://localhost:7273/api/Attraction/Attractions");
    }
    }


    And my call in my component in ngOnInit:

    constructor(private attractionService: AttractionService) { }

    async ngOnInit(){
    let y;
    await this.attractionService.getAtttractions()
    .then(x => {
    y = x;
    })
    console.log(y);
    }


    Currently I getting in console.log this:

    Observable {source: Observable, operator: ƒ}


    How to get my structure (api call is working fine).

    Continue reading...

Compartilhe esta Página