0

I'm working on a Lightning Web Component which calls an API from Apex and display the response in the component with the help of a wrapper class. The response is displaying correctly in the apex method. But once it is returned to the LWC's JavaScript method, it throws the following error:-

"{"ok":false,"status":400,"statusText":"Bad Request","body":{}}"

LWC

myMethod(event) {
    myMethod({ accountId: this.recordId, name: event.detail.value })
    .then((result) => {     
        console.log(result); // NEVER EXECUTED
    })
    .catch((error) => {
        console.log('error = ' + JSON.stringify(error)); // EXECUTED
    });
}

Apex

@AuraEnabled   
public static Api.ResultWrapper myMethod(Id accountId, String name) {
    Api.ResultWrapper  result = doThing(accountId, name);
    System.debug('result = ' + result); // CORRECT RESULT
    return result;
}

Api.ResultWrapper

public with sharing class Api {

    // INNER

    public class ResultWrapper {
        private String hiddenProperty;
        private Map<String, String> attributes;


        @AuraEnabled
        public String getProperty() {
            return hiddenProperty;
        }


        @AuraEnabled
        public String getAccountName() {
            return attributes.get('name');
        }

    }
}

It works fine when I serialize the response in apex and return it as a JSON string to the javascript. I would like to know if there is a better solution for this issue other than the serialization workaround.

1 Answer 1

1

You need to annotate @AuraEnabled the properties of wrapper class

@AuraEnabled   
public static Api.ResultWrapper myMethod(Id accountId, String name) {

Here you are returning Api.ResultWrapper, meaning this is the data type that you want to read in javascript(lightning). If the properties of this data type are not Aura Enabled, it cannot be sent in the response. If you were using getProperty or getAccountName method and returning String, you will get it BUT if you want to return wrapper, you need aura enabled properties in that wrapper class and properties need to be public or global

public class ResultWrapper {
    @AuraEnabled private String hiddenProperty;
    @AuraEnabled private Map<String, String> attributes;

Also, you are getting "{"ok":false,"status":400,"statusText":"Bad Request","body":{}}" because you are using attributes.get('name') but actually not setting proper attributes value before returning. Remove method getAccountName or pass proper value (through ResultWrapper constructor) to attributes

8
  • I've already annotated the properties in the wrapper class
    – dennisjsk
    Jul 25, 2019 at 11:25
  • Can u show wrapper class also Jul 25, 2019 at 11:29
  • U need to annotate private variables Jul 25, 2019 at 11:38
  • But the @AuraEnabled methods should take care of accessing them. right?
    – dennisjsk
    Jul 25, 2019 at 11:43
  • edited answer. You are not using those methods but returning the wrapper data type, so properties of that data type (wrapper) need to be aura enabled Jul 25, 2019 at 12:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .