When utilizing the code snippets below from two different components, I am able to receive a valid response value from the subscriber.
dataService.ts
fetchFormData(){
return this.http.get('http://localhost:48116/RecuruitmentService.asmx/addRoleTest?i=3').map(this.extractData);
}
private extractData(res: Response) {
return res.text() ? res.json() : {};
}
app.component.ts
this.dataService.fetchFormData().subscribe((response) => console.log(response));
console output
https://i.sstatic.net/blH9o.png
However, when attempting to assign the response value to a variable, I encounter an 'undefined' error.
myvar:any;
this.dataService.fetchFormData().subscribe(response => this.myvar = response);
console.log(this.myvar);
I have reviewed the following discussions. Solution-1 , Solution-2
Despite this, the issue remains unresolved. Any suggestions on how to tackle this problem?