javascript - 使用议程时出现 UnhandledPromiseRejectionWarning

标签 javascript node.js agenda

我按议程包编写了一些代码,但在运行我的项目时遇到了 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');

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

最佳答案

请在像这样使用'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');
})(); 

关于javascript - 使用议程时出现 UnhandledPromiseRejectionWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460679/

相关文章:

javascript - 对底层 npm 包进行更改

node.js - NodeJS 议程调度程序 : cluster with 2 or 3 workers, 作业没有均匀地获得 "distributed"

node.js - 在 nodejs 中创建重复事件以更新或插入 MySQL 表

javascript/D3 基础知识 : If asynchronous call, 为什么代码在 json 请求之外?

node.js - 如何使用 request.js 将网络图像通过管道传输到 s3 上传链接

javascript - 如何在 promise angularjs 中访问数据

node.js - 带有 docker-compose : Container ID 166535 cannot be mapped to a host ID 的 Bitbucket 管道

javascript - 类型错误 : Cannot read property 'id' of undefined in nodejs

javascript - jQuery AJAX 调用不返回数据

javascript - 使用按值删除元素的自定义方法扩展数组