I'm running a function, and when I enter data correctly it does exactly what I need it to, but when there is invalid data provided I want to return an error message to the user. As you can see in the pic, the request returns the message I want to display but I cannot access it!
My AngularJS controller:
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;
});
}
I've tried logging error, res, response, message and they all come give me an error. If I remove the console.log lines it pops up an alert with the FAILED AVP CHECK message. I want that to be custom, with the message showing in the response window.
Tracing backwards, my data factory console.logs just fine:
function complete(response) {
console.log("data factory response SUCCESS!");
return response;
}
function failed(error) {
console.log("API error response: ", error);
}
When I look in the console, just I see the error object I could access, but can't seem to do.
The part of my NodeJS function sending the error:
return res.status(422).send({
message: 'Missing parameters! Need name and AVP ID'
});
The factory has problems:
ERRONEOUS
function failed(error) { console.log("API error response: ", error); }
function failed(error) {
console.log("API error response: ", error);
//RE-THROW error
throw error;
}
When a promise error handler fails to re-throw an error, the function returns undefined
. This converts the rejected promise to a fulfilled promise with a value of undefined
.
For more information, see