In my code, I have initialized a variable inside the constructor like this:
constructor(public http: HttpClient) {
this.data = null;
this.http.get(this.url).subscribe((datas: any) => {
this.dbUrl = datas[0].db_url2;
console.log(this.dbUrl) // <- output here
})
}
The output that I see is:
987456321
Later, in a different method within the same class, I reference the variable again:
getDetails() {
let headers = new HttpHeaders();
headers.append('Content-Type','application/json');
console.log(this.dbUrl); // <- expected output here
return this.http.get(this.dbUrl + 'details', { headers: headers})
}
However, when I try to display the output, it shows as Undefined
. The dbUrl
is declared as a global variable. Can anyone assist me with solving this issue?