0

I have the following lwc code:

import findLocalityApex from '@salesforce/apex/NACController.findLocalityApex'
...
findLocality() {
        console.log('this.lat findLocality' + this.lat);
        console.log('this.lng findLocality' + this.lng);
        findLocalityApex({lat: this.lat, lng: this.lng})
        .then(data => {
            console.log('Locality');
            console.log(data);
            if(data.locality.numberLocality != null) {
                this.localityNumberFromCallout= data.locality.number;
            }
        })
        .catch(error => {
            this.error = error;
            console.log(' error ', this.error);
        });
      }
...

where findLocalityApex is the backend apex method in NACController class which calls the ResponseNACCallout class which does the the callout.

The NACController class:

...
@AuraEnabled
public static ResponseNAC findLocalityApex(String lat, String lng) {
    ResponseNAC respNac = ResponseNACCallout.responseNacCallout(lat, lng);

    return respNac;
}
...

In the ResponseNACCallout which does the callout I instantiate ResponseNAC respNAC = new ResponseNAC(); and after mapping the callout response with the respNAC, I simply return respNAC to the calling class which is the NACController class's findLocalityApex method, which does a return to the LWC.

The ResponseNAC class:

public with sharing class ResponseNAC {

    public Locality locality = new Locality();
    public NACError nacError;

    public class Locality {
        public Integer numberLocality;
        public String romanNumeralLocality;
        public Integer IdLocality;
    }

    public class NACError {
        private String message;

        public NACError(String message) {
            this.message = message;
        }
    }
}

Now when I check the debug logs it returns the correct result, but in the lwc it is always undefined. The order seems to be as follows:

  1. this.lat findLocality
  2. this.lng findLocality
  3. then goes back to where it was called where it console logs other stuff
  4. And then enters the .then of findLocalityApex method and console logs the string Locality and an empty object and then goes into .catch where it throws the error.

Its out of order because it is asynchronous. But why I am not able to capture the result in the lwc?

10
  • What does the findLocalityApex method return? Jan 19, 2021 at 18:29
  • an apex class that is used for mapping with the callout results
    – nSv23
    Jan 19, 2021 at 18:31
  • 1
    No, we need to see what the return value is. Obviously, it's an Apex class, but that doesn't describe the structure of the class.
    – sfdcfox
    Jan 19, 2021 at 19:22
  • Does it correctly include AuraEnabled annotations, for example?
    – Phil W
    Jan 19, 2021 at 19:23
  • 1
    None of the response attributes have @AuraEnabled annotations and without these the properties are not going to appear in the payload sent to the client.
    – Phil W
    Jan 19, 2021 at 20:30

1 Answer 1

3

Without the @AuraEnabled annotation on attributes the attributes are not included in the JSON response. To remedy this, update your response class thus:

public with sharing class ResponseNAC {
    @AuraEnabled
    public Locality locality = new Locality();

    @AuraEnabled
    public NACError nacError;

    public class Locality {
        @AuraEnabled
        public Integer numberLocality;

        @AuraEnabled
        public String romanNumeralLocality;

        @AuraEnabled
        public Integer IdLocality;
    }

    public class NACError {
        @AuraEnabled
        public String message;

        public NACError(String message) {
            this.message = message;
        }
    }
}

Be aware that public attributes don't always work with LWCs and you may need tonuse a public property (with getter and setter) instead.

You must log in to answer this question.

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