【问题标题】:Cannot access AngularJS $http error response data无法访问 AngularJS $http 错误响应数据
【发布时间】:2019-09-23 02:22:20
【问题描述】:

我正在运行一个函数,当我正确输入数据时,它完全符合我的需要,但是当提供的数据无效时,我想向用户返回一条错误消息。正如您在图片中看到的,请求返回了我想要显示但我无法访问它的消息!

我的 AngularJS 控制器:

 vm.avpLogic = function(last, avp_id) {
      console.log("Checking AVP Registration");
      alert("Checking AVP Registration");
      registrationDataFactory.checkAVPReg({ last: last, id: avp_id })
      .then(function(response){
        if(!response) {
          console.log(error);
          alert(error);
        }
          console.log(" frontend RESPONSE ");
          console.log(response);
          vm.avp = response;
          vm.addPlayer(vm.registration, vm.avp)
      })
      .catch(function(response){
      // THE RESPONSE COMES BACK HERE FOR INVALID DATA
        console.log(response.body);  // THROWS ERRORS, see pic
        console.log("An error occurred, but we don't know how to log it.", message);
        alert("FAILED AVP CHECK.  Name and ID must match exactly, and they must be active during the rumble.");
        return error;
      });
    }

我试过记录错误、res、response、message,它们都给我一个错误。如果我删除 console.log 行,它会弹出一个带有 FAILED AVP CHECK 消息的警报。我希望这是自定义的,并在响应窗口中显示消息。

回溯,我的数据工厂console.logs就好了:

function complete(response) {
    console.log("data factory response SUCCESS!");
    return response;
}

function failed(error) {
    console.log("API error response: ", error);
}

当我查看控制台时,我只看到了我可以访问但似乎无法访问的错误对象。

我的 NodeJS 函数发送错误的部分:

   return res.status(422).send({
    message: 'Missing parameters!  Need name and AVP ID'
  }); 

【问题讨论】:

    标签: node.js angularjs angular-promise angularjs-http


    【解决方案1】:

    工厂有问题:

    错误

     function failed(error) {
        console.log("API error response: ", error);
    }
    

    更好

    function failed(error) {
        console.log("API error response: ", error);
        //RE-THROW error
        throw error;
    }
    

    当 promise 错误处理程序未能重新抛出错误时,该函数返回 undefined。这被拒绝的 Promise 转换为具有 undefined 值的已履行的 Promise。

    有关详细信息,请参阅

    【讨论】:

    • 谢谢你!!!其他人用 Angular 编写了代码,而我正在尝试各种事情。我的数据工厂来自几年前的一个教程,我没有像我应该的那样经常查看代码。没有人解释过我可以抛出这样的错误。 :D。终于有答案了。
    猜你喜欢
    • 1970-01-01
    • 2015-04-29
    • 2023-03-04
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2016-04-05
    • 2017-11-25
    • 2017-04-10
    相关资源
    最近更新 更多