How to send an error alert in UI whenever an error occurs on server side in nodejs?

风流意气都作罢 提交于 2021-01-29 15:48:27

问题


I am sending the request via AJAX to the server. I want to send the response to the AJAX call back whenever an error occurs. So for that in callback function I am writing this return code inside the error block. res.end('{"msg" : "Some error occurred", "status" : 700}');

And with status I am throwing an alert on UI with this message.

But whenever the code fails or some errors occurs the console do writes the console error in console but also the server stops and failed to reload the page again and I have to run npm start again. But it don't return the msg and status back to AJAX call as final response.

How to handle the code to achieve this?

Means whenever any error occurs I want to send an error alert on UI instead of full blockage of the server and webpage.

 router.route('/addTasks-approve').get(function(req, res) {

                      User.updateMany({'Addtasks.commonID':req.query.id},
                         {$set: {"Addtasks.$.width" :'250px',
                          "Addtasks.$.height" :'32px',
                          "Addtasks.$.background" :'linear-gradient(45deg, #0f3443, #34e89e)',
                          "Addtasks.$.border_radius" :'10px / 5px',
                         "Addtasks.$.status" :req.query.text}},
                        function (error, success) {
                              if (!error) {
                                console.log("Approved color set!");
                                User.findOne({tag:'Admin','Addtasks.commonID':req.query.id},function (error, dataAdmin) {
                                      if (error) {
                                          console.log("error = "+ error);
                                          res.end('{"msg" : "Some error occurred", "status" : 700}');
                                      }
                                      else {
                                        dataAdmin.Addtasks.forEach(element => {
                                          if(element.commonID == req.query.id)
                                          {
                                     User.findOneAndUpdate({tag:'Client','Addtasks.commonID':req.query.id},
                                     {$push: {'Addtasks.$.Bigpaths4Clients':{$each : element.Bigpaths4Clients}},
                                      $set: {"Addtasks.$.background" :'linear-gradient(45deg, #1E6305, #BDFF00)',
                                             "Addtasks.$.status" :'Done'}},
                                        function (error, data) {
                                              if (error) {
                                                console.log("error = "+ error);
                                                res.end('{"msg" : "Unable to add the Task", "status" : 700}');
                                              }
                                              else {
                                                console.log("Addtasks added to Client's dashboard succesfully");
                                                sendMails2Client(data.email, element.topic, 'In Progress', 'Done');
                                                sendMails2User(dataAdmin.email, element.topic, 'Done', 'Approved');
                                                User.findOne({tag:'Admin','Addtasks.commonID':req.query.id},function (error, dataWriter) {
                                                      if (error) {
                                                          console.log("error = "+ error);
                                                          res.end('{"msg" : "Some error occurred", "status" : 700}');
                                                      }
                                                      else {
                                                        sendMails2User(dataWriter.email, element.topic, 'Done', 'Approved');
                                                        res.end('{"success" : "success", "status" : 200}');
                                                      }
                                                    })
                                              }
                                      })
                                    }
                                  });
                                }
                              });
                              }
                              else {
                                res.end('{"msg" : "Unable to set the status and color for Approved", "status" : 700}');
                              }
                            });
                                                  });

来源:https://stackoverflow.com/questions/65809059/how-to-send-an-error-alert-in-ui-whenever-an-error-occurs-on-server-side-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!