'How to print an array inside object that is coming from database?
I am trying to print an array "data" inside an object "clientData" but the problem is I cant access the array inside before it loads from the database, it gives me an error "Property 'data' does not exist on type '{}'". How do I extract that array without specifying?
this.adnService.getClient().subscribe((result) => {
if (result === null) {
this.empty = true;
}
this.clientData = result;
console.log(this.clientData);
console.log(this.clientData.data[0].client_id);
})
Below is the clientData stringify output
{
"status": "pass",
"data": [
{
"client_id": "u616672",
"client_name": "Client 123"
}
]
}
Solution 1:[1]
this.adnService.getClient().subscribe((result) => {
if(result && result != ""){
this.clientData = result;
console.log(this.clientData);
console.log(this.clientData.data[0].client_id);
}else{
this.empty = true;
}
})
(or)
console.log(this.clientData?.data[0].client_id);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muthulakshmi M |
