Below is the server side code where I am manually throwing an error. It get caught inside catch block. But Now I want to send tat caught error back to the AJAX error field and throw an alert on UI.
What I am doing wrong?
$.ajax({
type: "GET",
url: "/update-sources",
dataType: "json",
data: {
selectedValue: text,
commonIDs: id,
Id4AddtasksBigpaths:Id4AddtasksBigpaths
},
contentType: "application/json",
success: function(response){
if (response.status == '200') {
$.LoadingOverlay("hide");
swal({
title: "In Progress",
text: "Status set to 'In Progress'",
icon: "success",
button: "Ok",
showCancelButton: true
}).then((result) => {
if (result) {
window.location.reload();
}
});
}
},
error: function (request, ajaxOptions, thrownError) {
$.LoadingOverlay("hide");
alert(request.status);
alert(thrownError);
}
});
Server side code:
router.route('/update-sources').get(async (req, res) => {
try {
var eleTopic, eleStatus;
let data = await User.findOne({tag:"Client","Addtasks.commonID":req.query.commonIDs});
throw new Error("Just a try error!");
let success = await User.updateMany({"Addtasks.commonID":req.query.commonIDs},
{$set: {"Addtasks.$.width" :'250px',
"Addtasks.$.height" :'32px',
"Addtasks.$.background" :'linear-gradient(45deg, #091B6A, #0029FF)',
"Addtasks.$.border_radius" :'10px / 5px',
"Addtasks.$.status" :req.query.selectedValue}});
if (success) {
console.log("In Progress color set!");
let dataWriter = await User.findOne({tag:"Writer","Addtasks.commonID":req.query.commonIDs})
dataWriter.Addtasks.forEach(async (element) => {
if(element.commonID == req.query.commonIDs)
{
await sendMails2User(dataWriter.email, element.topic, 'Requirement Submitted', 'In Progress');
let dataAdmin = await User.findOne({tag:"Admin","Addtasks.commonID":req.query.commonIDs})
await sendMails2User(dataAdmin.email, element.topic, 'Requirement Submitted', 'In Progress');
return res.end('{"success" : "Updated Successfully", "status" : 200}');
}
})
}
} catch (err) {
console.log(err)
return res.end('{"status" : 400}');
}
})
P.S. Currently the error is somehow not getting returned back to the AJAX error block and the overlay is keep displaying. (Not ending)
question from:
https://stackoverflow.com/questions/65907015/how-to-catch-return-error-in-ajax-from-server-side-nodejs-when-manually-throwing 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…