在 MEAN Web 应用程序中,从 Express(服务器端)发送 HTTP 响应错误 403、500,未在 Angu

In MEAN web app, sending HTTP response error 403, 500 from Express (server side), not captured in errorCallback function in Angular controller

本文关键字:错误 响应 HTTP Angu 未在 发送 服务器端 应用程序 Web MEAN Express      更新时间:2023-09-26

在我的 Node Web 应用程序中,我正在其中一个 Angular 控制器中发送一个 HTTP GET 请求,并且在 Express 中定义的相同路由中,在路由逻辑的某个地方,我正在发送 HTTP 500 响应(也尝试了 403 错误)。在 Angular 控制器中,此错误未在正确的回调函数中捕获,有什么见解吗?

myApp/public/javascripts/main_controller.js

mainApp.controller('myController', function($scope, $routeParams) 
{
    ...
    $scope.myFunction = function (profile) {
     $http({
        url: '/route1/" + username, 
        method: "POST"
      })
      .then(function successCallback(response) 
      {
        //ERROR: This is printed after the GET request  
        console.log("success updating likes");
      },  function errorCallback(response) 
      {
        //ERROR: This is NOT printed after the GET request 
        console.log("error: ", response.error);
     });
  };
});

myApp/routes/api.js

router.route('/route1/:username')
  .post(function(req, res) {
   User.findOne({'user': param1 }, function (err, user)
  {
     //...Logic to Find and update a property of collection
     if (user.property == condition)
     {
      res.status(500).send({ error: 'some error' });
     } 
    //...Other logic
  })
  });

虽然它无论如何都应该工作,但请尝试从 res.status 返回,否则您的代码将继续运行,如果您下面有更多 res,它可能会在某处感到困惑并发送多个标头。