I keep getting the following error:
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(). 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). (rejection id: 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?
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);
}
});
CodePudding user response:
The unhandled rejected promise probably comes from the Parse.Object.saveAll()
function since you are not awaiting this. 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);
}
});