1

Component:

<aura:component controller="LightningController" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global"  >
<aura:attribute name="purposes" type="ApexDataContainer" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    data: {!v.purposes}
</aura:component>

Controller:

    ({
        doInit : function(component, event, helper) {
            var getdata = component.get("c.JsonGetValue");
            getdata.setCallback(this, function(response){      
                var state = response.getState();
                if (state === 'SUCCESS'){
                    var result = response.getReturnValue();
                    console.log('1'+JSON.stringify(result));
                    console.log('2'+result['purposes']);
component.set("v.purposes",response.getReturnValue());
                }
            });
            $A.enqueueAction(getdata);      
        }
    })

APEX:

public class LightningController {

    @AuraEnabled
    public static ApexDataContainer JsonGetValue() {
        ApexDataContainer c1 = new ApexDataContainer();
    System.debug('hello');
        String json1=           '{'+
        ''+
        '    "purposes": ['+
        ''+
        '        {'+
        ''+
        '            "purpose": "service-improvement",'+
        ''+
        '            "legalGround": "ic",'+
        ''+
        '            "status": "not_answered",'+
        ''+
        '            "description": ['+
        ''+
        '                {'+
        ''+
        '                    "language": "sv-EN",'+
        ''+
        '                    "text": "Service data structure"'+
        ''+
        '                }'+
        ''+
        '            ],'+
        ''+
        '            "version": "1.0.0",'+
        ''+
        '            "dataIds": []'+
        ''+
        '        }'+
        '    ]'+
        ''+
        '}';       
        c1 = (ApexDataContainer)JSON.deserialize(json1,ApexDataContainer.class);

        system.debug('wrapper'+c1.purposes.size());
        return c1;
    }
}

In apex debug log, I see that the json object has been successfully parsed, while in a lightning controller, I dont get the value in response.getreturnvalue for this apex method (JsonGetValue) in the console.log itself

I saw in a couple of blogs that we can pass an object from APEX and refer in lightning controller and component

Can someone help me understand why I am not getting the object in the lightning controller console log

  • If you have the JSON then you can directly return it. – itzmukeshy7 Mar 30 '18 at 1:20
  • the string that i passed over there will be received from an integration callout . the string is the sample response so I have to deserialize as per my class and then i have to show the values in the lightning component – Quddus Mar 30 '18 at 11:48
5

Any object member that you return to Lightning needs to be @AuraEnabled.

This means that in your ApexDataContainer class, you need to add this annotation to all variables.

Eg:

public class ApexDataContainer {
    @AuraEnabled
    public PurposesContainer[] purposes {get;set;}
}


public class PurposesContainer {
    @AuraEnabled
    public String purpose {get;set;}
    @AuraEnabled
    public String legalGround {get;set;}
    ///etc etc
}

I'm not sure what you are trying to do, but that string that you defined seems to be a difficult way to do it!

| improve this answer | |
  • I will add the aura annotation and see if that works. But yes to answer your question about the string, the string that i passed over there will be received from an integration callout . the string is the sample response so I have to deserialize as per my class and then i have to show the values in the lightning component – Quddus Mar 30 '18 at 11:47
0

Caspar Harmer is correct, any member that you want to be visible to the Aura view components will need @AuraEnabled keyword. Once you do that you will get the Object in the Controller JS. Then you can use native JavaScript method JSON.parse to get the details.

({
    doInit : function(component, event, helper) {
        var getdata = component.get("c.JsonGetValue");
        getdata.setCallback(this, function(response){      
            var state = response.getState();
            if (state === 'SUCCESS'){
                var result = response.getReturnValue();
                var rootObj = JSON.parse(JSON.stringify(result));
                var purposesObjArray = rootObj.purposes;
                var purposesObj = JSON.parse(JSON.stringify(purposesObjArray[0]));
                console.log("purpose : "+purposesObj.purpose);
                console.log("legalGround : "+purposesObj.legalGround);
                console.log("status : "+purposesObj.status);
                var descriptionObj = JSON.parse(JSON.stringify(purposesObj.description[0]));
                console.log("description-language : "+descriptionObj.language);
                console.log("description-text : "+descriptionObj.text);
                console.log("version : "+purposesObj.version);
                component.set("v.purposes",JSON.stringify(purposesObj));
            }
        });
        $A.enqueueAction(getdata);      
    }
})
| improve this answer | |
  • Thanks Yes, thats true i missed adding @auraenabled which makes me getting null value in the component. Secondly, I think since I am already returning an object type back to the Js controller so I don't need to stringify and then parse (JSON.parse(JSON.stringify(result));), I will be able to directly access those values in the controller – Quddus Mar 30 '18 at 20:41

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.