https://i.sstatic.net/i5vFM.jpg
I recently received some data from a request, expecting to return a JSON object that I can utilize in HTML. However, upon running
console.log(this.data);
I noticed that there are 20 elements in the articles array, but it returns as undefined or null when I execute
console.log(this.data.__zone_symbol__value);
I would greatly appreciate any assistance in resolving this issue. :)
Edit made in response to comment:
ngOnInit() {
this.url = 'https://newsapi.org/v2/top-headlines?' +
'country=us&' +
'apiKey=c6aecd1cd1de49edaca2544076713c45';
this.Newsdata = this.FetchHeadlines(this.url);
console.log(this.Newsdata);
console.log(this.Newsdata.__zone_symbol__value);
}
get(url: string): Promise<any> {
return new Promise((resolve, reject) => {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = () => {
if (req.status === 200) {
resolve(req.response);
} else {
reject(Error(req.statusText));
}
};
req.onerror = () => {
reject(Error('Network Error'));
};
req.send();
});
}
FetchHeadlines(url: string): Promise<any> {
console.log("Entered FetchHeadlines");
return this.get(url).then(JSON.parse).catch(error => {
console.log('getJSON failed for', url, error);
throw error;
});
}