0

它说 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) {}
}
};
4

1 回答 1

2

您传递给的函数forEach不是异步的,不建议asyncforEach.

将 async/await 与 forEach 循环一起使用

于 2021-09-22T11:30:36.263 回答