1

I retrieve JSON from an API:

public with sharing class BeerRequests {

@AuraEnabled
public static BeerJsonParser getRandomBeer() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('http://prost.herokuapp.com/api/v1/beer/rand');
    request.setMethod('GET');
    HttpResponse response = http.send(request);
    if (response.getStatusCode() == 200) {
        BeerJsonParser suggestedBeer = BeerJsonParser.parse(response.getBody());
        return suggestedBeer;
    }

    return null;
}

I parse it to an Apex object using a generated class BeerJsonParser

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
public with sharing class BeerJsonParser {
    public class Definition {
        public String key;
        public String title;
    }

    public String key;
    public String title;
    public String synonyms;
    public String abv;
    public String srm;
    public String og;
    public List<String> tags;
    public Definition brewery;
    public Definition country;


    public static BeerJsonParser parse(String json) {
        return (BeerJsonParser) System.JSON.deserialize(json, BeerJsonParser.class);
    }
}

The JSON is being retrieved (server side) correctly and an Apex object is created as expected.

I want to show this data in my lightning app.

<aura:component controller="BeerRequests">
    <aura:attribute name="beerinfo" type="BeerJsonParser" />
    <aura:handler name="init" action="{! c.doInit }" value="{! this }" />

    <ui:outputText value="{! v.beerinfo.title }" />
</aura:component>

And use the following init to load the data

({
    doInit: function(component, event, helper) {
        var action = component.get("c.getRandomBeer");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.beerinfo", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

The response in the controller (client side) is valid and state is success. But the getReturnValue function (client side) returns an empty object instead of the expected object. This object is NOT empty on the server side.

Question: Why am I unable to return the BeerJsonParser object to my lightning app? I did not create a custom object BeerJsonParser in the SalesForce Object Manager.

  • By object definition you mean an apex class with field names? No. You can also choose to get the response as a string. the object definition makes it easier for parsing the response received. – abhi Jan 10 '17 at 13:57
  • No, by object definition I meant a custom object, created in the SalesForce Object Manager UI. Which defines which fields are available for this object type. – Stijn Jan 10 '17 at 14:03
  • BeerJsonParser is not aura enabled – abhi Jan 10 '17 at 17:23
  • Thanks abhi for the info, I found this post explaining how to aura enable the object: salesforce.stackexchange.com/questions/53596/… – Stijn Jan 11 '17 at 11:31
1

The return object has to be Aura Enabled to be displayed in the a lightning component. All the attributes which has to be made visible need the @AuraEnabled annotation, see the question below:

@AuraEnabled Support for Apex Class Return Types?

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.