Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

node.js - How to catch/return error in AJAX from server side nodejs when manually throwing error inside try block?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...