Lightning response.getReturnValue() question

  • I did not find much info on the "response.getReturnValue" function and what it exactly returns. I understand that this is the function that must be used when a Client Side JS controller calls a Server Side action. The returned value must therefore be whatever the server returned.

    My question: Is it a JSON? How can I inspect what is there inside, from the Web console?

    So far I have been unsuccessful in doing so.

    This is my Apex Controller:

    public class CurrencyController {
    @AuraEnabled
    public static List<CurrencyType> getCurrencies() {
        return [SELECT Id, IsoCode, LastModifiedDate FROM CurrencyType ORDER BY createdDate ASC];
    }
    }
    

    This is my JS Controller

    ({
      doInit: function(component, event, helper) {      
      var action = component.get('c.getCurrencies');
    
    // Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
     component.set('v.currencies', actionResult.getReturnValue());
        console.log('currency data is:' + actionResult.getReturnValue());
    });
    $A.enqueueAction(action);
    }
    })
    

    But what is logged is this:

    currency data is:[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

    There are 34 Currency items in DB and there are understandably 34 items in this array. But what is there inside these objects? Why is there just two [object Object] when I am picking up 3 fields? I tried changing the number of fields and the log never changes.

    I understand this is an extremely foolish question but I just am not able to get it. Please excuse my lack of lightning and Javascript knowledge.

    Any insight is much appreciated.

  • Keith C

    Keith C Correct answer

    4 years ago

    It is a JavaScript object - the JSON has already been parsed.

    To inspect simple JavaScript objects use JSON.stringify:

    console.log('currency data is:' + JSON.stringify(actionResult.getReturnValue()));
    

    This will walk around the object graph - across arrays and nested objects - and output each object found in string format.

    (The server takes Apex objects, turns them into JSON strings, sends them to the client, then the client turns the JSON into JavaScript objects that are passed to your code. For this debugging you are just flipping back to JSON to view the data.)

    Another way of looking at the data sent to the server and returned from the server is to use your browser's Network tab in its Developer tools. There you can see the data that is sent and received, and by looking through the multiple requests find the right ones and pick out the JSON that is transmitted.

    If the JSON is large, you can cut and can paste it into a formatter e.g. JSON Formatter & Validator to more easily examine it.

    Thanks a lot for your answer, really great for understanding :)

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM