# Axios Returning Blank Objects

**URL:** <https://community.weweb.io/t/axios-returning-blank-objects/6001>\
**Category:** Ask us anything\
**Created:** [December 18, 2023, 9:53pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001 "2023-12-18T21:53:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 9:53pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/1 "2023-12-18T21:53:06Z")

</div>

All of the functions below either error out or return a blank object from the jsonplaceholder testing endpoint. Tested the URL in Postman and all results come back fine and it also works in HTTP.

const url = ‘[https://jsonplaceholder.typicode.com/posts](https://jsonplaceholder.typicode.com/posts)’;

async function fetchPhotos() {  
try {  
console.log(“Sending request to:”, url);  
const response = await axios.get(url);  
console.log(“Response received:”, response);  
return response.data;  
} catch (error) {  
console.error(“Error during fetch:”, error);  
return { error: error.message };  
}  
}

async function main() {  
console.log(“Calling fetchPhotos…”);  
const data = await fetchPhotos();  
console.log(“Data returned:”, data);  
if (data && data.error) {  
console.log(‘Error:’, data.error);  
} else if (data) {  
console.log(‘Fetched Data:’, data);  
} else {  
console.log(‘No data fetched’);  
}  
}

main();

* * *

const url = ‘[https://jsonplaceholder.typicode.com/posts](https://jsonplaceholder.typicode.com/posts)’;

async function fetchPhotos() {  
try {  
const response = await axios.get(url);  
return response.data; // Returns the data array from the response  
} catch (error) {  
console.error(error);  
return { error: error.message }; // Returns an error object if the Axios call fails  
}  
}

// Correctly awaiting the async function  
async function main() {  
try {  
const data = await fetchPhotos();  
if (data.error) {  
console.log(‘Error:’, data.error);  
} else {  
console.log(‘Data:’, data);  
}  
} catch (error) {  
console.error(‘Error in main function:’, error);  
}  
}

main();

* * *

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 10:07pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/2 "2023-12-18T22:07:33Z")

</div>

Reproduction videos:

> **[weweb-1.mp4](https://drive.google.com/file/d/1Hgglsa4Ekbtv1Mcd1IbclnTB-facCgNW/view?usp=sharing)**
>
> Google Drive file.

> **[weweb-2.mp4](https://drive.google.com/file/d/1L_f51L8kS6DI13hmtu3uwIsTn2XwYrJd/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [December 18, 2023, 10:34pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/3 "2023-12-18T22:34:56Z")

</div>

usually the error itself can have better hints than a video, maybe share the error that you get as text.  
And of course if this is in a js action you need to actually rerurn something from it. Think about it as the body of an async function, you want to return something, not only invoke other functions.

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 10:38pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/4 "2023-12-18T22:38:36Z")

</div>

@dorilama The function below is returning a 200:  
const url = ‘[https://jsonplaceholder.typicode.com/posts](https://jsonplaceholder.typicode.com/posts)’;

async function fetchPosts() {  
try {  
console.log(“Sending request to:”, url);  
const response = await axios.get(url);  
console.log(“Response received:”, response);  
return response.data; // This will be an array of post objects  
} catch (error) {  
console.error(“Error during fetch:”, error);  
return { error: error.message };  
}  
}

async function main() {  
console.log(“Calling fetchPosts…”);  
const data = await fetchPosts();  
console.log(“Data returned:”, data);  
if (data && data.error) {  
console.log(‘Error:’, data.error);  
} else if (data) {  
console.log(‘Fetched Data:’, data);  
} else {  
console.log(‘No data fetched’);  
}  
}

main();

But the object that comes back is:

No data fetched  
(Empty object)

No errors appear in the logs.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/f/f3875d0ba2f707adc7f435adc9077f91fc8d682c.jpeg)

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 10:39pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/5 "2023-12-18T22:39:29Z")

</div>

> [@geenius-cloud](#):
>
> return response.data; // This will be an array of post objects

Yes, the return.response.data is in the function.

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [December 18, 2023, 10:50pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/6 "2023-12-18T22:50:48Z")

</div>

it is indeed in one of the function that you invoke but the action itself is not returning anything.  
This is very easy once you get it. Forget about the actual data fetching and return a sample array, then add the fetching.

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 11:06pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/7 "2023-12-18T23:06:49Z")

</div>

@dorilama

So this is now an exploratory guessing game? Any reason why you are not able to just share the solution?

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [December 18, 2023, 11:10pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/8 "2023-12-18T23:10:33Z")

</div>

> [@geenius-cloud](#):
>
> So this is now an exploratory guessing game? Any reason why you are not able to just share the solution?

The solution is to add a return statement from the root of your code, I literally said that 3 times now.  
Here you go: `return yourdata`

FYI copy pasting lines of code is not a solution and it makes you waste time, understanding what you are doing it’s the solution. 👋

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 11:32pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/9 "2023-12-18T23:32:31Z")

</div>

This is still returning a blank object:

const url = ‘[https://dummyjson.com/posts](https://dummyjson.com/posts)’;  
const fetchPosts = async () =\> {  
const response = await axios.get(url);  
return response.data;  
};  
const data = await fetchPosts();

The example script itself provided by WeWeb is returning a blank object with [https://dummyjson.com/posts](https://dummyjson.com/posts)

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 18, 2023, 11:37pm UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/10 "2023-12-18T23:37:30Z")

</div>

So does modifying the response to dot target posts instead of data:

const url = ‘[https://dummyjson.com/posts](https://dummyjson.com/posts)’;

const response = await axios.get(url);

return response.posts;

The output JSON is:  
{  
“posts”: [  
{  
“id”: 1,  
“title”: “His mother had always taught him”,  
“body”: “His mother had always taught him not to ever think of himself as better than others. He’d tried to live by this motto. He never looked down on those who were less fortunate or who had less money than him. But the stupidity of the group of people he was talking to made him change his mind.”,  
“userId”: 9,  
“tags”: [  
“history”,  
“american”,  
“crime”  
],  
“reactions”: 2  
},  
{…},  
{…}  
// 30 items  
],  
“total”: 150,  
“skip”: 0,  
“limit”: 30  
}

---

<div class="post-metadata">

**Author:** ![raydeck](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/raydeck/32/384_2.png) [@raydeck](https://community.weweb.io/u/raydeck)\
**Post date:** [December 19, 2023, 2:57am UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/11 "2023-12-19T02:57:17Z")

</div>

@geenius-cloud You have two problems:

1. The main() function lacks a return statement. You need `return data` after getting it from `fetchPosts`.
2. The outer code lacks a return statement. You need to insert the word return before main(), e.g. `return main()

Take care of those and you’re probably good to go. Let us know if you have ongoing issues!

Also, @dorilama is the single most expert person on here who is not part of the weweb team. He is also the nicest in helping people with their hard problems. He is not paid to help on this community (nor am I). Let’s all be at least half as nice to him so we can keep this wheel of learning turning for all of us!

---

<div class="post-metadata">

**Author:** ![geenius-cloud](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/geenius-cloud/32/6746_2.png) [@geenius-cloud](https://community.weweb.io/u/geenius-cloud)\
**Post date:** [December 19, 2023, 5:15am UTC](https://community.weweb.io/t/axios-returning-blank-objects/6001/12 "2023-12-19T05:15:55Z")

</div>

Thank you @raydeck and @dorilama, it now makes sense and works.

To be perfectly honest, I always prefer to side on the nice or neutral side, but I did see a couple of @dorilama responses in other threads and his tone there and in this thread made me feel small and somewhat “stupid”, with an overall “elitist” demeanor, and his responses certainly were not decorated with any kind of traditional social decorum, so my initial reaction was not the most positive to say the least.

Sure no one has to be nice or polite or provide any more info than necessary in contexts like this forum, but then one shouldn’t expect unequivocal niceties in return either 🤷🏼‍♂️.

But again, thank you both for taking the time to help me work through this very basic and fundamental concept, and yes I complete agree that one should have an understanding of what is happening in the development process instead of just seeking copy/paste responses.
