3

The response return value from my apex class is:

var value = JSON.stringify(response.getReturnValue());

{
  "LegalLegitimation":"0233XXXXX",
  "LegalLegitimationReadOnly":"0244XXXXXX",
  "NaturalLegitimation":"0255XXXXX",
  "NaturalLegitimationReadOnly":"0266XXXXXX"
}

Now, how can I access this object and use it in my lightning component? Please help

3 Answers 3

7

If you return a map, you don't need to parse or stringify the value.

Note that if you write this in JavaScript:

console.log('return value: '+response.getReturnValue());

You'll get:

return value: [object Object]

That's just how JavaScript's toString() method works on objects.

To see what's in the return value, try this:

console.log(response.getReturnValue());

To access the individual values, use either the dot-notation or index notation:

var value = response.getReturnValue();
console.log(value.LegalLegitimation);
console.log(value['LegalLegitimation']);
2
  • Exactly, this is what I need. Thank you Commented May 22, 2019 at 14:57
  • Thanks, this is exactly what I was looking for! You're a legend
    – Fitz
    Commented Sep 28, 2021 at 21:09
5

Use JSON.parse(response.getReturnValue()) to parse your response return value as js object. then you can access the properties of this js object using either dot notation or bracket notation:

const returnedObj = JSON.parse(response.getReturnValue());
returnedObj.LegalLegitimation; // = '0233XXXXX'
returnedObj['LegalLegitimationReadOnly']; // = '0244XXXXXX'
4
  • I get the value of returnedObj.LegalLegitimation as undefined Commented May 22, 2019 at 13:09
  • It depends on the Type of the return object via the apex controller. If you do serialize it before returning, you should parse it in your js controller, If you don't serialize it - don't parse its value. Just try const returnedObj = response.getReturnValue(); Commented May 22, 2019 at 13:15
  • My type of the return object is Map<String, Id> getLegitimationRecordTypes and I did not serialize it. I tried with just response.getReturnValue(); and I get [object Object] and therefore the value of LegalLegitimation as undefined. Commented May 22, 2019 at 13:20
  • you mentioned in above comments you get [object Object] from response.getReturnValue(); that means you get array of key value pair hee key and value also both are objects. can you update your question with apex method and response bind to which type of aura attribute?
    – sdandamud1
    Commented May 22, 2019 at 13:31
0

The OP asked how to use a Map returned from Apex, presumably retaining the object type as a Map in Javascript. The Apex response will be an object of key/value pairs, just as the OP shows in his example. Lightning supports ES2017 so we can do the following:

/*
value = {
  "LegalLegitimation":"0233XXXXX",
  "LegalLegitimationReadOnly":"0244XXXXXX",
  "NaturalLegitimation":"0255XXXXX",
  "NaturalLegitimationReadOnly":"0266XXXXXX"
}
*/

const map = new Map(Object.entries(value));
map.get('LegalLegitimation'); // '0233XXXXX'

You must log in to answer this question.

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