I am looking to return some data from an asynchronous .map() function. It's async due to the axios call being inside of it to return some data. I am trying to .map() through an array [] inside files.kitchen and then call axios to get the base64 of each image URL inside files.kitchen. I have tried logging the data using await due to the asynchronous behavior of my function. The await returns undefined each time. I know I am calling it right by using await. However, I could have a syntax error. I've studied each relative question on here and none of them have worked for me.
Here is my code.
const getbase64Data = () => {
files.kitchen
? files.kitchen.map(async (image, index) => {
const response = await axios.get(image, {
responseType: "arraybuffer",
});
const buffer = Buffer.from(response.data, "binary").toString(
"base64"
);
const data =
"data:" + response.headers["content-type"] + ";base64," + buffer;
return data;
})
: null;
};
console.log(await getbase64Data());
Please let me know if I need to share anything else.
returnin yourgetbase64Data()function.