I have this variable test1 = []
, I push some res.json
to it.
I expect it to be something like this [[{json,json}]]
, but turns out it gives me undefined when I try to iterate it like this test1[0]
.
Can someone explain to me how is test1[0]
undefined? And how can I get values from it?
Below is the chrome console output for this variable.
snippet: test1 = data_array
var data_array = []
const BookingHistory = getBookings(userDbID).then((json) => {data_array.push(json)})
console.log("test1", data_array[0]) //undefined
getBookings:
export const getBookings = (userDbId) => {
const id = userDbId; //one of the global state that is sent to Date from App
// Create our request constructor with all the parameters we need
const request = new Request(`/users/${id}/bookings`)
return fetch(request)
.then((res) => {
if (res.status === 200) {
return res.json();
}
})
.then((json) => {return json})
.catch((error) => {
console.log(error);
});
}