0

In my code I am trying to update Account record. But the update is failed, how do i handle the error in my lightning component.

I tried this in helper:

var state = response.getState();
if(state=="SUCCESS"){

}else if(state=="ERROR"){
  var errors = response.getError();
// setting this error to on of the attribute of the component
}

NOTE: addError method @ field level in apex method cannot be handled in lightning. One of the limitations with respect to handling errors in lightning

4

Well, assuming that you have a reference to the component, then you would simply set the error to your attribute like this:

component.set("v.errAttribute", JSON.stringify(errors));

This is assuming that your errors attribute is a simple string, define something like:

<aura:attribute name="errAttribute" type="String" />

You could also extract the message from each error object and put that in an array of strings to render also:

<aura:attribute name="errorMessages" type="String[]" />

Javascript:

var errors = response.getError();
var errorMsgs = errors.map(function(error){
    return error.message;
});
component.set("v.errorMessages",errorMsgs);

Finally, you'd render them using an iteration:

<ul>
    <aura:iteration items="{!v.errorMessages}" var="message">
        <li>{!message}</li>
    </aura:iteration>
</ul>
| improve this answer | |
1

Here is how I debug error

Code Snippet

 } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
| improve this answer | |
  • This I Tried but not working.Thank u so much for replying :) – Sahana Gowdar Sep 22 '16 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.