1

In the below code, I could see only the finish event emitting and not the field event.

const Busboy = require('busboy');

module.exports.controller = function(app) {
    app.post('/api', function(req, res) {
        var busboy = new Busboy({ headers: req.headers });
        var formdata = {};
        busboy.on('field', function (fieldname, val) {
            console.log("fieldname : " + fieldname);
            console.log("value : " + val);
            formdata[fieldname] = val;
        });
        busboy.on('finish', function() {
            res.send(formdata);
        });
        req.pipe(busboy);
    });
};

As response, I receive {} back in postman. What could be possibly going wrong here?

Aiswarya
  • 49
  • 6
  • Do you actually have that code or do you have more code? Found this on busyboy issue tracker: https://github.com/mscdex/busboy/issues/158. Better search for the word "field" there and start reading. Maybe you find your answer. – Jorge Fuentes González Jan 17 '18 at 01:31
  • This is my complete code. – Aiswarya Jan 17 '18 at 01:32
  • I saw that you have a low ratio of questions made/accepted answers. That's the main reason of this site to live so I have to ask you to start increasing your ratio if you want to receive more help, as people will see that you don't reply to answers that are trying to help you and won't answer you this time or in the future. – Jorge Fuentes González Jan 17 '18 at 01:45
  • Oh! I see. But I never hesitate to accept answers when it solves my problem. – Aiswarya Jan 17 '18 at 06:53
  • 1
    I have solved the problem by removing the unwanted entry for this API in my swagger.json file. I guess swagger do not support busboy. Hence all the problem. – Aiswarya Jan 19 '18 at 11:28

1 Answers1

0

I know this is long overdue, but I had this issue too and then I realized I was missing the name="" field in my HTML inputs.

Working snippet:

// handle form submissions
router.post('/submit', (req, res) => {
    var busboy = new Busboy({ headers: req.headers });

    busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        console.log('Field [' + fieldname + ']: value: ' + val);
    });

    busboy.on('finish', () => {
        res.writeHead(303, { Connection: 'close', Location: '/' });
        res.end();
    });

    req.pipe(busboy);
});
Zane Helton
  • 1,044
  • 1
  • 14
  • 34