gpt4 book ai didi

javascript - 在 MEAN Web 应用程序中,从 Express(服务器端)发送 HTTP 响应错误 403、500,未在 Angular Controller 的 errorCallback 函数中捕获

转载 作者:行者123 更新时间:2023-12-03 07:34:08 26 4
gpt4 key购买 nike

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

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 返回,否则你的代码将继续运行,并且如果你下面有更多资源,它可能会在某个地方变得困惑并发送多个 header 。

关于javascript - 在 MEAN Web 应用程序中,从 Express(服务器端)发送 HTTP 响应错误 403、500,未在 Angular Controller 的 errorCallback 函数中捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35689548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com