0

Let's say we have

@wire (getPPM, {accountId: '$recordId'}) getPPM(data) {
        console.log(data);
        if(data && typeof data.summaries!== 'undefined') {
           console.log(data.summaries);
       }

    }

in console i can see:

{data: {…}, error: undefined}
.   data:
.   summaries: (2) [{…}, {…}]

but data.summaries is always undefined. what am i doing wrong?

public with sharing class PPMDataWrapper {
public String org62AccountId;
@AuraEnabled public List<Summaries> summaries;

/**
 * @description inner class for Program Summaries 
 */
public class Summaries {
    @AuraEnabled public Map<String, String> measurements;
    @AuraEnabled public String programType;
    @AuraEnabled public String totalPoints;
    @AuraEnabled public String programSummaryId;
    @AuraEnabled public String rankName;
    @AuraEnabled public String startDate;
    @AuraEnabled public String endDate;
    @AuraEnabled public String periodName;
}
5
  • It looks like you're using a wrapper class to return data? Can you show us what that looks like? Commented Feb 7, 2021 at 12:45
  • 1
    Your variable data contains two fields, data and error. So you should use data.data.summaries. You may want to rename your variable to result or something similar. Commented Feb 7, 2021 at 12:52
  • as i said i can see in developer console data object Commented Feb 7, 2021 at 12:52
  • Yes, data is an object, containing two members, called data and error. The data part contains the actual returned data. It's confusing because you named your variable data as well. Commented Feb 7, 2021 at 12:54
  • we clearly see 2 entries in summaries how is it undefined your way of accessing it is wrong Commented Feb 7, 2021 at 13:23

1 Answer 1

1

As rael_kid pointed out you're accessing summaries in wrong way. Your param is named data and it has data and error property. So to access summaries you should write

console.log(data.data.summaries);

As a good practice and to avoid confusion, you should use JavaScript Object Destructuring assignment for wired method param. Here's updated wired method with JS Object Destucturing syntax.

@wire(getPPM, {accountId: '$recordId'})
getPPM({data,error}) {
    if(!error){
       console.log(data.summaries);
    }else{
       console.error(error);
       //or perform additional error handling
    }
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.