When I'm trying to return my res.data from my function and then console.log it I get undefined but if I console.log it from inside the function I get the normal result
const getDefaultState = () => {
axios
.get("http://localhost:5000/urls/todos")
.then((res) => {
if (res.data) {
console.log(res.data);
return res.data;
}
})
.catch((err) => console.log(err));
};
console.log(getDefaultState());
so I get first
(3) [{…}, {…}, {…}]
(the normal value) but then from outside I get
undefined
undefinedbecause when you console.log(getDefaultState()), your request is still processing. See thisgetDefaultStatewill never return the "real" value. The best you can do is have it return a promise.