将流式电子邮件数据添加到流星中的MongoDB

时间:2018-07-29 14:59:06

标签: mongodb meteor stream busboy

我有一条路由设置为从SendGrid接收Webhook,该Webhook发送MultiPart /表单数据。我可以使用busboy将各种字段输出到控制台中,但是我在努力解决最后一个难题:将经过解析的数据放入Collection对象中(或者如果不熟悉流星,则仅放入MongoDB中)。

我认为类似以下内容的方法会起作用,但是数据库中的数据数组始终为空,我想我缺少了解流何时完成的关键步骤?

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();
  }));

0 个答案:

没有答案