Attempting to extract specific information from an API response has proven challenging. Despite my efforts to isolate the desired data, all listed details appear as undefined
.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AnyArray } from 'mongoose';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient) { }
fetchCountryData (country: string) {
let formattedCountry = country.split('-').join(' ');
let api1 = `http://api.worldbank.org/v2/country/${formattedCountry}?format=json`;
let api2 = ``
return this.http.get(api1)
}
setCountryData(country: string) {
let subject = new Subject();
this.fetchCountryData(country).subscribe((data: any) => {
subject.next({
country: data.name,
capital: data.capitalCity,
income: data.incomeLevel,
region: data.adminregion,
latitude: data.latitude,
longitude: data.longitude,
})
})
return subject.asObservable();
}
}
The API response contains:
(2) [{…}, Array(1)]
0
:
{page: 1, pages: 1, per_page: '50', total: 1}
1
:
Array(1)
0
:
adminregion
:
{id: 'EAP', iso2code: '4E', value: 'East Asia & Pacific (excluding high income)'}
capitalCity
:
"Beijing"
id
:
"CHN"
incomeLevel
:
{id: 'UMC', iso2code: 'XT', value: 'Upper middle income'}
iso2Code
:
"CN"
latitude
:
"40.0495"
lendingType
:
{id: 'IBD', iso2code: 'XF', value: 'IBRD'}
longitude
:
"116.286"
name
:
"China"
region
:
{id: 'EAS', iso2code: 'Z4', value: 'East Asia & Pacific'}
[[Prototype]]
:
Object
Struggling with errors in specifying the array format due to integer labels. Seeking assistance for resolving this issue.