1

Although my code works fine, the console throws the following warning:

API resolved without sending a response for /api/image-upload

The endpoint itself uploads an image to Digital Ocean's object storage service "Spaces", and sends back some metadata (like the location):

import formidable from "formidable-serverless";
import aws from "aws-sdk";
import fs from "fs";

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async (req, res) => {
  const s3 = new aws.S3({
    endpoint: "...",
    accessKey: "...",
    secretKey: "...",
  });

  const form = new formidable.IncomingForm();

  form.parse(req, async (err, fields, files) => {
    if (err) return res.status(500);

    const path = files["file[]"].path;
    const file = fs.readFileSync(path);
    const name = files["file[]"].name;

    s3.upload({
      Bucket: "my-bucket",
      ACL: "public-read",
      Key: `${...}/${name}`,
      Body: file,
    }).send((err, data) => {
      if (err) return res.status(500);
      fs.unlinkSync(path);

      res.json({
        file: {
          url: data.Location,
        },
      });
    });
  });
};

1 Answer 1

1

You can avoid this warning by disabling the warning for unresolved requests.

/api/image-upload

export const config = {
  api: {
    externalResolver: true,
  }, 
}

Custom config

Or wrap form.parse into a promise:

const formFields = await new Promise(function (resolve, reject) {
    form.parse(req, (err, fields, files) => {
        if (err) {
            reject(err);
            return;
        }
        resolve({fields, files});
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.