0

I have a route set up to receive a webhook from SendGrid, which sends MultiPart/form data. I can get the various fields to output in the console with busboy, but I'm struggling to fill in the final piece of the puzzle: getting this parsed data into a Collection object (or just into MongoDB if not familiar with meteor).

I thought something like the following would work, but the data arrays in the db are always blank, i'm guessing i'm missing a crucial step in knowing when the stream has finished?

WebApp.connectHandlers.use('/applicants', (req, res, next) => {
  let body = '';

  req.on('data', Meteor.bindEnvironment(function (data) {
    body += data;

    let bb = new Busboy({ headers: req.headers });
    let theEmail = [];

    bb.on('field', function(fieldname, val) {
      console.log('Field [%s]: value: %j', fieldname, val);
      let theObject = [];
      theObject[fieldname] = val;
      theEmail.push(theObject);

    }).on('error', function(err) {
      console.error('oops', err);
    }).on('finish', Meteor.bindEnvironment(function() {
      console.log('Done parsing form!');

      // try to add data to database....
      Meteor.call('applicants.add', theEmail);
    }));

    return req.pipe(bb);
  }));

  req.on('end', Meteor.bindEnvironment(function () {
    res.writeHead(200);
    res.end();
  }));
aroundtheworld
  • 731
  • 1
  • 5
  • 15
  • I use formidable for this https://www.npmjs.com/package/formidable and it works pretty simply, just gives you all the data for the form without needing to set up a bunch of callbacks. – Mikkel Jul 30 '18 at 00:08
  • Thanks @Mikkel - I ended up switching to Mailgun who provide a parsed email from the outset instead of SendGrid who only provide Multipart/form webhooks. – aroundtheworld Jul 31 '18 at 08:17

0 Answers0