I've come across this weird behavior from fetch.
First look at this screenshot of the lengths (which are correct) and actual structures: console screenshot
Consider the following json response from a server, where the likes
array clearly contains one object, and the comments
array contains two:
{
"status":"success",
"payload":{
"272699880986":{
"likes":[
{
"createdTime":"2016-11-07T04:41:21.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"likes":1
}
],
"comments":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
]
}
}
}
Now, passing that response through the whole response.json()
, like this:
fetch('http://example.com/entries.json', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
...authKeys,
}
})
.then(response => {
switch(response.status){
case 200:
console.log('status 200', response)
return response.json()
}
})
.then(json => {
console.log('parsed response')
console.log(json)
})
Gets me the following result:
{
"status":"success",
"payload":{
"272699880986":{
"likes":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"likes": 1,
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
],
"comments":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
]
}
}
}
See how it mixed up the likes
and comments
collections. This is driving me insane right now. Am I missing something?
UPDATE
Here's a gif showing chrome's console listing showing and array containing one object, and another one containing two objects. When I click the former, it turns into an array with two objects! This baffles me