[英]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.