3

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.

6

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 :) – SFDC Dev 27 Jan 11 '18 at 11:07
0

You will have declared the attribute in your component.

<aura:attribute name="currencies" type="CurrencyType[]" />

It returns the result of your query as objects of type CurrencyType made up of the requested fields. You can output the result in a loop at the component level:

<aura:iteration var="varCurrency" items="{!v.currencies}"> 
    <p>{!varCurrency}</p>
    <p>{!varCurrency.Id}</p>
    <p>{!varCurrency.IsoCode}</p>
    <p>{!varCurrency.LastModifiedDate}</p>
</aura:iteration>

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.