没有理由使用
bind
在这里,尤其是没有绑定一个从未使用过的参数数组。也,
schedulerLog
是一个方法,一个属性
this
,而不是局部变量-这就是为什么会出现异常。您只需要使用闭包和箭头函数来保持
这
上下文:
initScheduler() { // no need for async here
try {
let dt = new Date(el.scheduled_time)
let db = this.knex
this.schedule.scheduleJob(dt, async () => { // arrow function!
let sc = new ScheduledContent(db)
el.status = "sent"
let res = await sc.createOrUpdateContent(el.id, el.title, null, el.scheduled_time, el.image, el.status)
// ^^^^^ use async/await instead of .then() here
this.schedulerLog('Job-ID #' + el.id + ' -- ' + el.title + ' -- executed at: ' + dt + " -- and updated: " + res);
// ^^^^^ call method on this
});
this.schedulerLog("\n Number of Jobs Scheduled: " + Object.keys(this.getQueue()).length + "\n");
} catch (error) {
this.schedulerLog(error);
}
}