After creating an Angular function in typescript to make an http request for JSON data and save it to an object, I noticed that the function requires two clicks of the associated button to work properly. Although the connection and data parsing are successful, the initial click does not trigger the functionality as expected.
Code snippet for the function:
test: any;
callData(){
this.http.get('http://127.0.0.1:8000/api/?keyword=the&source=1&format=json')
.subscribe(data => {this.test = data});
//.subscribe(data => {this.new_test.push(data)});
for (let i of this.test){
console.log(i['id'], i['title'], i['description'])
}
}
When initially clicking the corresponding button to activate the function, the following is observed: https://i.sstatic.net/lrSI1.png
However, upon clicking the button again, everything seems to function correctly. https://i.sstatic.net/zZuPA.png
I suspect that the issue lies with the datatype being utilized, but my goal is to employ a data structure that can store all elements returned from the GET request so that I can easily access and log each one individually.console.log()