1

I have a function in my lwc that gets data from Apex Controller. My code:

JS:

getServiceResourceInfo({srIdList: this.professionalIdList})
        .then(result => {
            console.log('TEST result ->' + JSON.stringify(result));
            result.forEach(element => {
                const sr = { id: element.id,
                    label: element.name + element.isActive
                };
                this.professionals.push(sr);
            });
        })

return type from APEX is a Wrapper like this:

public class SRInfoWrapper{
    String id;
    String isActive;
    String name;
}

WRAPPER FROM APEX CONTROLLER DEBUG (JUST 1 LINE BEFORE RETURN):

(SRInfoWrapper:[id=xxxxxxxxAE, isActive=Inactive, name=xxxxxxxxxx05], SRInfoWrapper:[id=xxxxxxxxxDgCAM, isActive=Active, name=xxxxxxxxxxx06])

Problem is that 'TEST result ->' prints [{},{}]. In UI, data is shown as 'NaN'. If I stringify the result before forEach, UI does not show anything.

0

1 Answer 1

1

You need to use @AuraEnabled, make the properties public, and include a default getter and setter:

public class SRInfoWrapper{
    @AuraEnabled public String id { get; set; }
    @AuraEnabled public String isActive { get; set; }
    @AuraEnabled public String name { get; set; }
}

Also, as a general rule, you can use Array.prototype.map to make your code cleaner:

this.professionals = result.map((element) => 
  ({ ...element, 
     label: `${element.name}${element.isActive}`
  }))
1
  • It worked. Thank you so much, you are in quite questions. You are big!
    – CPS
    Feb 8, 2022 at 10:10

You must log in to answer this question.

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