0

Here is a small Azure function, I am trying to get post form fields using node formidable.

    module.exports = function (context, req, intable) {
    var formidable = require('formidable');
    var form = new formidable.IncomingForm(),
        fields = [];

    ----> form.parse(context.req, function(err, fields, files) {
        context.log(fields);
        context.done();
        return;
    });
    return;
};

In the line with arrow in code, it gives an error TypeError: req.on is not a function. Not sure what to do, any help will be appreciated.

imrn
  • 297
  • 2
  • 12

2 Answers2

4

As you are using Azure ready available packages won't help you because in classic web server file arrives in a chunk on a server so 'on' and 'end' is used to retrieve data on a server. In case of Azure functions, Azure will parse the form for you and append in req.body object and this object is JSON, not any request object so 'on' and 'end' function won't be available.

So, You need to parse the form data manually.

I am working in AWS & Azure both (Generally I used text files to upload through AWS and Azure) so I created one until to parse the form data which you can use to parse your own.

You can use the same function. I updated it on GitHub.

https://github.com/chandani-volansys/multipart-data-parser/blob/master/multipart.js

Chandani Patel
  • 451
  • 4
  • 12
1

The method you are calling here in formidable expects the require('http') req object - however the request object here is not the same (as you can see, doesn't have like req.on() or other methods). I'm not familiar enough with this package to know if theres a way you can provide a request body (what this req object has) and still parse or not.

Details on the req object being passed into this function are here

jeffhollan
  • 3,139
  • 15
  • 18