Amit-A / formidable-serverless

Enables use of formidable (node.js module for parsing form data, especially file uploads) in serverless environments.
https://www.npmjs.com/package/formidable-serverless
MIT License
35 stars 2 forks source link

TypeError: this._parser.write is not a function #4

Closed potravniy closed 4 years ago

potravniy commented 4 years ago

I like formidable-serverless, it really solved my pain with Firebase functions. It is used as following:

const formidable = require("formidable-serverless");

function getBodyFields(req) {
  var form = new formidable.IncomingForm();
  return new Promise((resolve, reject) => {
    form.parse(req, (err, fields) => {
      if (err) reject(err);
      else if (Object.keys(fields).length === 0) reject(new Error("No data to upload."));
      else resolve(fields);
    });
  });
}

api.post(apiPath, async (req, res) => {
  try {
    const { base64, width, height, ext } = await getBodyFields(req);
  } catch (error) {
    console.error(error);
    res.status(500).send(error);
  }
});

Having sent an empty request without any field and file the unhandled error happened:

TypeError: this._parser.write is not a function at IncomingForm.write (/functions/node_modules/formidable/lib/incoming_form.js:159:34) at IncomingForm.formidable.IncomingForm.parse (/functions/node_modules/formidable-serverless/lib/index.js:58:10) at Promise (/functions/api.js:114:10) at new Promise () at getBodyFields (/functions/api.js:113:10) at api.post (/functions/api.js:31:50) at Layer.handle [as handle_request] (/functions/node_modules/express/lib/router/layer.js:95:5) at next (/functions/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/functions/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/functions/node_modules/express/lib/router/layer.js:95:5)

"Unhandled" means that error was not cought with neither if (err) reject(err); nor if (Object.keys(fields).length === 0) reject(new Error("No data to upload."));

It would be good to catch this error in callback function as err.

Thank you!

Amit-A commented 4 years ago

Yes, it would be good to catch the error in the callback function.

Looks like the parent formidable repo fixed this issue recently: https://github.com/node-formidable/node-formidable/commit/c8bba1b1d661f198c477d1ec64bc31efbba194a3#diff-38e857ca3aca5cc608230f4e8d0b3ea5

I will push an update soon.

Amit-A commented 4 years ago

This should fix it.

Amit-A commented 4 years ago

Published updated version (v1.0.3) to npm.