[Solved] Confused with bindEnvironment

I want to run the following code

  const files_stream = Readdirp ({
    root : folder,
    entryType : 'files',
    fileFilter : info => !FilesAlreadyRead.findOne(info.name)

where Readdirp is an npm package to read recursively all the files in a folder.

That code raises a ‘cannot run ouside of a fiber’ error message. My understanding from the Meteor Guide was that the solution was to wrap my Meteor call with Meteor.bindEnvironment

  const files_stream = Readdirp ({
    root : folder,
    entryType : 'files',
    fileFilter :  Meteor.bindEnvironment (info => !FilesAlreadyRead.findOne(info.name))

But that code is not doing anything. I have tried to print the value returned by the collection but it never prints anything

Meteor.bindEnvironment(info => {
   const v = FilesAlreadyRead.findOne(info.name)
   console.log("meteor", info.name, v)
   return !v

My workaround till now was to save all the files returned by Readdirp in an array, use a future to wait till the readdirp completes and filter it afterwards in a Meteor code. But the amount of files on the server has grown so much it doesn’t work anymore.

It might be that you’re using bindEnvironment incorrectly. Check the docs.

I don’t know if this is your problem, but the call to find returns a cursor… you should be using findOne instead which will either return null or the first matching doc. Your use of bindEnvironment seems right to me…

I don’t think so. He’s providing an info parameter and then referencing info.name. Looking at the docs:

updateGitHubFollowers() {
    user: 'stubailo'
  }, Meteor.bindEnvironment((err, res) => {
    // Everything is good now

Though I may be wrong. The documentation on bindEnvironment is pretty scarce.

Good catch but the code in the application does a findOne. I just made a mistake copying it. I have updated the examples with findOne

The answer is bindEnvironment works but it’s SO SLOW the result appears in the console after 13h !