0

I have set up a cron job using the npm package cron. I am trying to do the following Coll.find().forEach function but I am getting the error Error: Can't wait without a fiber

    var job = new CronJob({
      cronTime: '00 09 11 * * 1-5',
      onTick: function() {
            var userIds = []

            Coll.find().forEach(function(doc) {
                    userIds.push(doc._id)
            });
      },
      start: false,
      timeZone: "Europe/London"
    });
   job.start();

I have been using npm packages fibers and future library. I still got the same error.

var resultOne = collFind();


  function collFind() {
     var f = new future()
     var userIds = []

     Coll.find().forEach(function(doc) {
        userIds.push(doc.userId)
    });

    return f['return']({userIds:userIds}
    return f.wait()
  }
meteorBuzz
  • 3,110
  • 5
  • 33
  • 60

1 Answers1

2

Try using Meteor.bindEnvironment.

var job = new CronJob({
  cronTime: '00 09 11 * * 1-5',
  onTick: Meteor.bindEnvironment(function() {
        var userIds = []

        Coll.find().forEach(function(doc) {
                userIds.push(doc._id)
        });
  }),
  start: false,
  timeZone: "Europe/London"
});
job.start();

It will ensure that the callback get's run in the current fiber, and ensure all global variables are accessible.

nathan-m
  • 8,875
  • 2
  • 18
  • 29