ionic capacitor android emulator can't fetch API data from local nodejs server

Viewed 29

I am unable to fetch any API data from my local Nodejs server using android emulator.

http:

url: 'http://localhost:8000/'

this.data = (await this.http.get<Res>(url + '/' + id).toPromise()).data;

Will work when fetching API as a web app.

ionic serve

Will Not Work when I run my app using android emulator.

via: ionic capacitor run android -l --external

I get an Error:

ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"http://localhost:8000/","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:8000/: 0 Unknown Error","error":{"isTrusted":true}} at resolvePromise (polyfills.js:

1 Answers

Create a service file.

export class ClassService {
  constructor(
    public httpClient: HttpClient
  ) {}

 getData(): Observable<any> {
    return this.httpClient
      .get<any>(`url + '/' + id`)
      
  }

}

Go wherever you want to call the data and call this service.

 constructor(
public service: ClassService 
  ) {}
this.service.getData().subscribe(x=> console.log('Data here', x)

Related