javascript - 从 Node 中的 CronJob 调用 API 没有响应

标签 javascript node.js cron slack-api

我正在尝试从 CronJob 中调用 slackbot API,我希望在一天中的某个时间向 channel 发送一条消息,但从 CronJob 中调用时,相关方法不起作用。相关代码如下:

Shiftbot.prototype.run = function () {
    Shiftbot.super_.call(this, this.settings);

    this.on('start', this._onStart);
    this.on('message', this._onMessage);
};

Shiftbot.prototype._onStart = function() {
    this._loadBotUser();
    new CronJob('* * 16 * * *', function(){
        console.log('You will see this message every second ');
        Shiftbot.prototype._sendReminder();
    }, null, true, "America/New_York");
}

Shiftbot.prototype._sendReminder = function() { 
    console.log('test');
    this.postMessageToChannel("shiftbot_beta", "Cron test", {as_user: true});
}

上面的 _sendReminder 方法在从另一个 Shiftbot.prototype.* 方法调用时工作正常,但不能从 cronjob 内部调用。任何帮助将不胜感激!

最佳答案

在 Shiftbot.prototype 范围内创建变量 self 并在 CronJob 范围内使用它,如下所示:

Shiftbot.prototype._onStart = function() {
    var self = this;
    this._loadBotUser();
    new CronJob('* * 16 * * *', function(){
        console.log('You will see this message every second ');
        self._sendReminder();
    }, null, true, "America/New_York");
}

关于javascript - 从 Node 中的 CronJob 调用 API 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47541552/

相关文章:

javascript,为什么不删除 var 关键字?

javascript - jquery 用 span 标签包围选定的文本和图像

javascript - 在主干中使用模板+ jquery ui

node.js - 在 Node JS 中为 WebSocket (ws) 使用自签名 CA 证书

javascript - 无法在客户端呈现 EJS 模板

linux - 如何使用 Crontab 详细创建新文件?

javascript - 使用 jQuery 查找事件表单元素

javascript - 将一个文件中的 mongoose 现有模式模型重用到另一个文件中的模式模型

docker - 在docker容器上运行crontab不运行?

php - 使用CURL与cronjob进行PHP多处理