【发布时间】:2020-12-08 07:21:01
【问题描述】:
我希望从异步 .map() 函数中返回一些数据。它是异步的,因为 axios 调用在其中返回一些数据。我正在尝试通过files.kitchen 内部的array [] 进行.map(),然后调用axios 以获取files.kitchen 内部每个图像URL 的base64。由于我的函数的异步行为,我尝试使用 await 记录数据。 await 每次都返回undefined。我知道我使用等待是正确的。但是,我可能会遇到语法错误。我已经研究了这里的每个相关问题,但没有一个对我有用。
这是我的代码。
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());
如果我需要分享其他内容,请告诉我。
【问题讨论】:
-
您的
getbase64Data()函数中没有return。
标签: javascript node.js asynchronous