await 仅在异步函数中有效,同时我已经将 "async" 放入函数中
await only valid in async function meanwhile i already put "async" in the function
它说 await 只对 async 函数有效,但我已经把 async 词放在 (req,res,next) 之前,我是不是写错了?
const axios = require("axios");
const FormData = require("form-data");
const imageKit = async (req, res, next) => {
if (req.files) {
try {
const files = req.files;
const values = Object.values(files);
values.map((perArray) => {
perArray.forEach((item) => {
if (item.mimetype === "image/jpeg" || item.mimetype === "image/png") {
const newForm = FormData();
const encodedFile = item.buffer.toString("base64");
newForm.append("file", encodedFile);
newForm.append("fileName", item.originalname);
const encodedKey = Buffer.from(
process.env.IMAGE_KIT + ":"
).toString("base64");
const result = await axios({
method: "POST",
url: "https://upload.imagekit.io/api/v1/files/upload",
data: newForm,
headers: {
...newForm.getHeaders(),
Authorization: `Basic ${encodedKey}`,
},
});
}
});
});
} catch (error) {}
}
};
你传递给forEach
的函数不是async,不建议在forEach
中使用async
。
它说 await 只对 async 函数有效,但我已经把 async 词放在 (req,res,next) 之前,我是不是写错了?
const axios = require("axios");
const FormData = require("form-data");
const imageKit = async (req, res, next) => {
if (req.files) {
try {
const files = req.files;
const values = Object.values(files);
values.map((perArray) => {
perArray.forEach((item) => {
if (item.mimetype === "image/jpeg" || item.mimetype === "image/png") {
const newForm = FormData();
const encodedFile = item.buffer.toString("base64");
newForm.append("file", encodedFile);
newForm.append("fileName", item.originalname);
const encodedKey = Buffer.from(
process.env.IMAGE_KIT + ":"
).toString("base64");
const result = await axios({
method: "POST",
url: "https://upload.imagekit.io/api/v1/files/upload",
data: newForm,
headers: {
...newForm.getHeaders(),
Authorization: `Basic ${encodedKey}`,
},
});
}
});
});
} catch (error) {}
}
};
你传递给forEach
的函数不是async,不建议在forEach
中使用async
。