【问题标题】:UnhandledPromiseRejectionWarning when using agenda使用议程时出现 UnhandledPromiseRejectionWarning
【发布时间】:2019-04-26 21:24:31
【问题描述】:

我按议程包编写了一些代码,运行项目时出现 UnhandledPromiseRejectionWarning。

我的代码在这里:

agenda.define('transferDBField', (job, done) => {
    if (this.tPrice) {
        this.prices.push(this.tPrice);
        done();
    }
    done();
});
agenda.every('1 days', 'transferDBField');

警告是这样的:

(node:1992) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findOneAndUpdate' of undefined
    at Agenda.module.exports [as saveJob] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\save-job.js:105:45)
    at Job.module.exports [as save] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\job\save.js:13:22)
    at createJob (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\every.js:29:15)
    at Agenda.module.exports [as every] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\every.js:56:24)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\models\product.js:32:8)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\routes\v1\home.js:3:13)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\routes\index.js:7:31)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
(node:1992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:1992) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

它的示例代码是这样的:

agenda.define('printAnalyticsReport', (job, done) => {
  User.doSomethingReallyIntensive((err, users) => {
    processUserData();
    console.log('I print a report!');
    done();
  });
});

agenda.every('15 minutes', 'printAnalyticsReport');

我像示例代码一样编写代码,但我不知道为什么会收到警告!

【问题讨论】:

  • 请注意,如果this.toPrice 是假的,您永远不会调用done()
  • @Bergi 我在 if 块之后完成了但我仍然有警告。我更新了我的帖子并发送了警告消息。
  • 这看起来像是议程库中的一个问题。 gheymat\app\models\product.js:32:8 是否引用了您发布的代码?
  • @Bergi 是的,第 32 行是这样的:agenda.every('1 days', 'transferDBField');
  • 如果您确定已正确初始化 agenda 对象(根据文档),我建议您向库提交问题。 (可能还是要提交一份关于非描述性错误消息的文件)。

标签: javascript node.js agenda


【解决方案1】:

请在使用 'every' 之前开始议程:

(async function() { // IIFE to give access to async/await
await agenda.start();

await agenda.every('15 minutes', 'printAnalyticsReport');

// Alternatively, you could also do:
// await agenda.every('*/15 * * * *', 'printAnalyticsReport');
})(); 

【讨论】:

    【解决方案2】:

    对我来说效果很好。更多信息请参考click here

    const Agenda = require('agenda')
    
    agenda.define('activer', async () => {
      await agenda.start()
      console.log(activer)
    })
    agenda.on('ready', () => {
      agenda.start()
      agenda.every('1 minute', 'activer')
    })
    

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2019-08-17
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多