WebApp.connectHandlers.use and access to collections

I am trying to implement a REST handler with WebApp.connectHandlers.use but can’t access any collections in the handler code. Does someone have a suggestion how to handle that?

Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment

What happened when you follow the suggested fix of wrapping the callbacks with Meteor.bindEnvironment ?

1 Like

Currently I have a very simple skeleton and I am at a loss what part to wrap. I actually am using Iron:Router and used to have a server route when I integrated with braintree, and that worked fine, but somehow I can’t get to the body of the request with Iron:Router, so I was trying this new approach

WebApp.connectHandlers.use('/stripe/webhooks', (req, res, next) => {
  console.log( `stripe/webhooks method=${req.method}` );
  let rawData = '';
  req.on('data', (chunk) => { rawData += chunk; });
  req.on('end', () => {
      const json = JSON.parse(rawData);
      // parse json and write result into collection
  });
  res.writeHead(200);
  res.end();
})

This is pretty old code, and we were using flow router, but it may be of help? This is what we ran to keep a local copy of webhook data.

WebApp.connectHandlers.use('/stripeHook', (req, res) => {
  // Basic security. Check the incoming hook versus our whitelisted stripe ips

  const ips = req.headers['x-forwarded-for'].split(',');

  const stripeIps = [
    '54.187.174.169',
    '54.187.205.235',
    '54.187.216.72',
    '54.241.31.99',
    '54.241.31.102',
    '54.241.34.107',
    '127.0.0.1', // For local testing
  ];
  const intersection = _.intersection(ips, stripeIps);

  if (intersection.length === 0) {
    res.setHeader('Content-Type', 'application/json');
    res.statusCode = 400;
    res.end('Not Found');
    return false;
  }

  // Next we dump our data and get a doc id to pass to the function(s) that need
  // to handle the data. If the dump is successful, we return a 200 so that
  // Stripe doesn't send us the hook again.

  const data = req.body;

  try {
    const docId = StripeData.insert({ ...data, createdAt: new Date() });
    if (docId) {
      res.setHeader('Content-Type', 'application/json');
      res.statusCode = 200;
      res.end('Thanks!');
    }
  } catch (err) {
    console.log('Stripe Hook Error', err);
  }
});
1 Like

Thank you very much for this code snippet, I will try to make that work. What I can already see different is your use of const data = req.body; where I was building rawData based on the data events on the request object. I will report back

1 Like

I’ll try wrapping the callbacks that will contain access to the collections

Unfortunately req.body is empty

I’m guessing you are using import { WebApp } from 'meteor/webapp'?

Also, I seem to remember having to use the solution here: Meteor webapp vs Picker vs simple:rest for REST API

I’m not sure why/how we moved away from that chunk parsing…