I am trying to create a task tracker. BackEnd is mySQL, API is built using Node JS and Express JS and frontend is React JS.
I am trying to assign a variable the response I get from the GET request.
In my frontend I am trying to assign the variable using the following code. As shown above, the get request works in PostMan.
const baseURL = 'http://localhost:5000/api/user/timesheet/13009';
let y = [];
axios.get(baseURL).then((response)=>{
for( var i in response.data){
y.push( response.data[i]);
}
console.log(y);
});
This is the output at the terminal. I can use indexes like y[0] and get that particular value. But when I remove the print statement and place it below the code, value of y changes.
const baseURL = 'http://localhost:5000/api/user/timesheet/13009';
let y = [];
axios.get(baseURL).then((response)=>{
for( var i in response.data){
y.push( response.data[i]);
}
});
console.log(y);
I get this output but y.length is set to 0 and I cant read the data in y.
const baseURL = 'http://localhost:5000/api/user/timesheet/13009';
let y = [];
axios.get(baseURL).then((response)=>{
for( var i in response.data){
y.push( response.data[i]);
}
});
console.log(y);
console.log('y.length');
console.log(y.length);
console.log('y[0]');
console.log(y[0]);
This is showing that there is no value for the index and the array is empty. What exactly is causing this issue and how can it be fixed?