Meteor.bindEnvironment with node stream function loses context

On calling a Meteor method with Meteor.bindEnvironment to create a new Fiber, the data context is no longer available. The insert ID returns undefined.'saveData', data, function(error, result){
    let returnValue = Collection.findOne({'data': result }).buffer;

  'saveData': function(data) {
    let doc = new PDFDocument();
    doc.image(data, 0, 0);
      function(buffer) {
        let newID = Collection.insert({
          'data': buffer,
  return newID;

I have attempted using Meteor.bindAsync because I thought I would have a new Fiber and the original data context, but I still get the error that Meteor must run within a Fiber. How can I bind newID to the original data context to pass it back once the insert has finished?

There’s a few things wrong with that method.

  1. let is block scoped: defining newID in your function{buffer) means it’s undefined outside of that function.
  2. You are declaring doc.end(); and return newID; entirely outside of the method.
  3. You will, I think, need to explicitly use Fibers/Futures to cope with that flow. Alternatively, consider using Promises and async/await and declare your method as async saveData(data) {.... Check out this article for more information on this approach.
1 Like