简体   繁体   English

即使使用 try catch 块,也会继续收到未处理的承诺拒绝

[英]Keep getting Unhandled promise rejection even with try catch block

I keep getting the following error:我不断收到以下错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 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().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ).要在未处理的承诺拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict (请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 2) (拒绝编号:2)

After looking up some solutions all I have to do is put my code within a try catch block, that you can see below but I still get this error, what am I doing wrong?在查找了一些解决方案之后,我所要做的就是将我的代码放在 try catch 块中,您可以在下面看到,但我仍然收到此错误,我做错了什么?

I am trying to update a column and set any arrays in it to an empty array.我正在尝试更新一列并将其中的任何数组设置为空数组。

Parse.Cloud.job("updateFollowedBy", async (request) => {
    try {
        var emptyArray = [];

        let date = new Date();
        let query = new Parse.Query("Categories");
    
        const results = await query.find({useMasterKey:true});
    
        results.forEach(object => {
            if (object.get("followedBy")) {
                object.set("followedBy", emptyArray);
            }
        });
        Parse.Object.saveAll(results);  
        return ("Successfully updated column on " + date); 
    } 
    catch(e) {
        console.log(e);
    }
});

The unhandled rejected promise probably comes from the Parse.Object.saveAll() function since you are not awaiting this.未处理的被拒绝的承诺可能来自Parse.Object.saveAll()函数,因为您没有等待它。 Try with:尝试:

Parse.Cloud.job("updateFollowedBy", async (request) => {
    try {
        var emptyArray = [];

        let date = new Date();
        let query = new Parse.Query("Categories");
    
        const results = await query.find({useMasterKey:true});
    
        results.forEach(object => {
            if (object.get("followedBy")) {
                object.set("followedBy", emptyArray);
            }
        });
        await Parse.Object.saveAll(results);
        // You might want to pass { useMasterKey: true } option to make it work without exception: 
        //await Parse.Object.saveAll(results, { useMasterKey: true });
        return ("Successfully updated column on " + date); 
    } 
    catch(e) {
        console.log(e);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在try / catch块上未处理的Promise拒绝 - Unhandled Promise rejection on try/catch block 未处理的 promise 拒绝与 Promise.all 解决并尝试/捕获 - Unhandled promise rejection with Promise.allSettled and try/catch 我不断收到未处理的承诺拒绝 - i keep getting unhandled promise rejection 如何在 Promise 捕获块中引发异常? (未处理的 promise 拒绝) - How to throw an exception in a Promise catch block? (Unhandled promise rejection) 尝试/捕获块和未处理的承诺异常 - Try/catch block and unhandled promise exception 当我已经在 catch 块中抛出错误时,得到未处理的 Promise 拒绝 - Getting unhandled Promise rejection when I already throw err in the catch block 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code Knexjs和es6是否尝试/捕获导致未处理的承诺拒绝警告? - Knexjs and es6 Try/Catch causing an Unhandled Promise Rejection Warning? 如何在try / catch错误处理中修复“未处理的promise拒绝错误” - How to fix “unhandled promise rejection error” in try/catch error handling 无法处理未处理的 Promise 拒绝使用 try-catch - Cannot handle Unhandled Promise Rejection using try-catch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM