After sending a request to the server and receiving a list of data, I encountered an issue where the data appears to be empty when trying to use it in another function within the same file.
The code snippet below initializes an array named tree
:
tree:any[]=[];
intialDataa(): any {
this.claimsManagerService.getAll(this.searchParam).subscribe(data => {
this.tree = data['records'];
})
}
However, when attempting to access this data in a different function:
openAdd(id, par, title, nodel): void {
console.log(this.tree)
}
The console output shows an empty array []
.
Further investigation revealed that during the IntialData()
function, the tree
array contains the following data:
(74) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
length: 0
__proto__: Array(0)
What could be causing this issue and how can I successfully access the tree
data?
Edit: Full Code:
intialDataa(): any {
// Code for initializing data...
this.loading = true;
// More code for retrieving and processing data...
this.tree = ddata;
}
Here is the second function:
openAdd(id, par, title, nodel): void {
// Code for opening and handling data...
}
HTMl Code :
// HTML code for displaying data...