I'm working on a portfolio project (am still a beginner) and I'm stuck almost at the start. I cannot handle the data I receive (though I have to say that I never saw such a JSON format until now).
Here is the code I try to convert to my needs:
Code sample
{
"data": {
"1": {
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"circulating_supply": 17199862,
etc...
}
},
"status": {
"timestamp": "2022-08-11T16:54:56.636Z",
"error_code": 0,
etc...
}
}
As you see I received an object and within that object, two nested object and so on. Sadly I worked with arrays until now so I struggle here a lot. I use the http get request to get the data:
fetchAllCryptos() {
const idParams = new HttpParams().set('string');
return this.http.get(
'API URL',
{
headers: {
...
},
params: idParams,
}
)
.pipe(
map((items) => {
const { data: dataObject, status: statusObject } = items;
console.log(dataObject, statusObject);
})
);
}
I tried to destructure the received object but I receive an error that "Property 'data' does not exist on type 'Object'." How come? I hard-coded the same received object into my project and destructured the object there and it WORKED:
constructor(private http: HttpClient) {
const { data, status } = this.object;
}
object = {
data: {
'1': {
id: 1,
name: 'Bitcoin',
symbol: 'BTC',
etc
Can anyone tell me what I'm doing wrong here?
CodePudding user response:
Your map
function doesn't return anything.
.pipe(
map((items) => {
const { data: dataObject, status: statusObject } = items;
console.log(dataObject, statusObject);
// TODO: return something
})
);
Or tap
instead map