0

In Next Js, I have a upload image endpoint.

const busboy = require('busboy')

export default function handle(req, res)
{
    const bb = busboy({headers: req.headers});
    bb.on('file', (fieldname, file, filename, encoding, mimetype) => 
    {
        res.status(200).send({ message: 'file' });  //doesn't do this
    });
    bb.on('close', () =>
    {
        res.status(201).send({ message: 'close' });
    });
    bb.on("finish", () => 
    {
        res.status(202).send({ message: 'finish' });
    });
    req.pipe(bb);
    res.status(203).send({ message: 'failed' });
}

I'm trying to parse form data but busboy dosen't seem to enter on file, close, or finish. It skips them and returns the 203 failed message.

This is my next onsubmit event which calls this enpoint

function onFileUpload(e)
  {
    e.preventDefault();
    let form = new FormData();
    form.append("image", file, file.name);
    console.log(form)
    axios({
      method: "post",
      url: "http://localhost:3000/api/uploadImage",
      data: form,
      headers: form.getHeaders
      //{'Content-Type': 'multipart/form-data'}
    })
    .then((res) =>
    {
      console.log(res);
    });
  }

Does anyone know what the issue is?

Bykaugan
  • 99
  • 6
  • Using `req.pipe()` doesn't magically make the code wait for any of the other event handlers before continuing to the next line, so your handler is _always_ returning a 203 status, no matter what. – robertklep Sep 29 '22 at 08:32

1 Answers1

0

I had a similar issue with Busboy on GCP cloud function. What solved my issue was adding bb.end(req.rawBody); after the "file" callback registration.