0

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

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']);
  • Exactly, this is what I need. Thank you – user3920709 May 22 at 14:57
3

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'
  • I get the value of returnedObj.LegalLegitimation as undefined – user3920709 May 22 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(); – Oleh Berehovskyi May 22 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. – user3920709 May 22 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 May 22 at 13:31

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.