1

I have this error when I tried use sleep for a loop

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

My Code:

   fs.readdir(directory, (err, files) => {
      if (err) {
        console.log("error get file");
      }

       if(files.length > 0) {
        for (var i = 0; i <files.length; i++) {
          Fiber(function()
          { 
            setTimeout( function()
            {
              Meteor.call('mymethodfile', files[i]);
            }, 1000 );
          }).run();
        }
      }
    });

in the same file

Meteor.methods({
    'mymethodfile':function(arch)
    {
      var file=arch;
      console.log(file);
      getdata(file);
    }
})

Please help me

1 Answers1

1

If you want to wrap an asynchronous function, you should you Meteor async wrap method or meteorhacks:async package, you can see the related answer here: Meteor: Proper use of Meteor.wrapAsync on server

If you want to use setTimeout, you have to use Meteor.setTimeout method instead: https://docs.meteor.com/api/timers.html

Community
  • 1
  • 1
bya
  • 33
  • 5