In my Angular 4 project, I am making two API calls within the ngOnInit lifecycle hook. My goal is to pass the data received from the first API call to the second one.
However, when I try to access the value in the second API method, it returns as "Undefined".
Here's a snippet of my code:
ngOnInit() {
this.http.get('https://api.ipify.org/?format=json').subscribe(
data => {
this.ip_Address = data['ip'];
console.log(this.ip_Address);
},
error => console.error(error)
);
this.http.get(`http://localhost:xyz/api/data/INSERT_USER_SESSION/?IP_Address=${this.ip_Address}&Time=${this.date_and_time}&state=${this.session_Begin_Status}`)
.subscribe(data => this.res = data['']);
}
Although the IP Address is logged successfully in the console, it appears as "undefined" when passed to the second API call. Can someone please point out where I might have made a mistake?