this is my first Stack Overflow post so bare with me if I formatted anything incorrectly.
This is my first Angular project and I can't assign data retrieved from the http.get().subscribe(...) method to my component class variable.
My component:
...
export class LeafletMapComponent implements OnInit, AfterViewInit {
data
constructor(private ps: PeopleService, private NgZone: NgZone) {}
ngAfterViewInit(): void {
console.log(this.allData) **DOESN'T WORK (logs undefined)**
}
ngOnInit(): void {
this.ps.httpGetData().subscribe( data => {
this.data= data
console.log(this.data) ***WORKS (logs all my data) BUT...***
})
console.log(this.data) ***THIS DOESN'T WORK... WHY? (logs undefined)***
}
}
My Service:
...
@Injectable({
providedIn: 'root'
})
export class PeopleService implements OnInit {
...
httpGetData() {
return this.http.get<any>(this.URL) **WORKS**
}
...
}
My question is: In this piece of code
ngOnInit(): void {
this.ps.httpGetData().subscribe( data => {
this.data= data
console.log(this.data) ***WORKS (logs all my data) BUT...***
})
console.log(this.data) ***THIS DOESN'T WORK... WHY? (logs undefined)***
}
Because I have already assigned my data to a class variable (this.data = data) inside the
.subscribe( data => {this.data = data})
method, I assume that I would be able to console.log(this.data) right after the subscribe method inside the ngOnInit() function, I would assume that this.data has already been initialized and can be accessed anywhere
I want to retrieve data as soon as my component loads, assign it to my class variable so my variable can be used in the ngAfterViewInit() function. However, inside the .subscribe() method, I am able to assign the returned data to my class variable "this.data" and log it, but, console.log(this.data) only works inside the .subscribe() method, nowhere else.
Also, does NgAfterViewInit() run before or after ngOnInit()? I am trying to access my class variable (this.data) from the NgAfterInit() function, but I must get the data from my service before I can do that...
Does this have to do with modifying Observable objects?
Edit: console.log(this.data) now works inside the ngOnInit function when I changed to a async function
async ngOnInit(): Promise<void>{
const res = await this.ps.httpGetData().toPromise()
this.data = res
}
But ngAfterViewInit won't detect the change because it is still logging undefined for the value this.data.
Please help and thanks in advance!