msavin / SteveJobs

A simple jobs queue that just works (for Meteor.js)
Other
207 stars 35 forks source link

[SOLVED] TypeError: this.success is not a function #65

Closed ScottAgirs closed 5 years ago

ScottAgirs commented 6 years ago

Upon calling my Meteor method doPO(); (containing SteveJobs method), I get the following console error output:

$ Ohh yeah - Success! This is the result from poToUser // console.log result (ohh yeah - no Success!, lol) $ $ $ Jobs: Job failed to run due to code error: poToUser $ $ TypeError: this.success is not a function $ at toolbelt.payoutToUser (imports/startup/server/stevejobs/po/registrars/Users.js:7:12) $ ..

Register job:

import { Meteor } from "meteor/meteor";
import { Jobs } from "meteor/msavin:sjobs";

Jobs.register({
  poToUser: () => {
      Meteor.call("sendEmailMethod"); **// this method gets executed and emails is delivered**
      this.success();
  }
});

Call the Job

import { Jobs } from "meteor/msavin:sjobs";
import { Meteor } from "meteor/meteor";

Meteor.methods({
  doPO: () => {
    Jobs.run("poToUser", {
      in: {
        second: 10
      },
      priority: 9999999999
    });
  }
});
ScottAgirs commented 6 years ago

It looks like this.success() needs to be inside an if statement, the code below works.

Jobs.register({
  poToUsers: function(userId, amount) {
    if (userId) {
      Meteor.call("sendEmailMethod", userId, amount, err => {
        if (err) console.log("ERROR: [po/registry.js]: ", err);
      });

      this.success();
      this.remove();
    } else {
      this.reschedule({
        in: {
          minutes: 5
        }
      });
    }
  }
});

Perhaps @msavin could elaborate on if this is true and maybe briefly why is this?

msavin commented 5 years ago

Yes its a JavaScript scoping issue.

You can do:

var self = this

And then call self.success() somewhere deeper in the function