I have a LWC where i want to iterate over different lists of Opportunities in a wrapper.
Currently im using:
import { LightningElement,wire } from 'lwc';
import OppRecords from '@salesforce/apex/kanbanLWCcontroller.getOpportunity';
export default class KanbanLWC extends LightningElement {
@wire(OppRecords) allOppsRecords;
}
Html file:
<template if:true={allOppsRecords}>
<template for:each={allOppsRecords.stage1.data} for:item="stage1Rec">
<c-single-opp-card opportunity={stage1Rec} key={stage1Rec.Id}></c-single-opp-card>
</template>
<template for:each={allOppsRecords.stage2.data} for:item="stage2Rec">
<c-single-opp-card opportunity={stage2Rec} key={stage2Rec.Id}></c-single-opp-card>
</template>
</template>
Apex controller:
public with sharing class kanbanLWCcontroller {
@AuraEnabled(Cacheable = true)
public static opportunityWrapper getOpportunity()
{
opportunityWrapper newWrap = new opportunityWrapper ();
for(Opportunity opportunities: [Select Id, Name,StageName, Amount, Account.Name, CloseDate FROM Opportunity])
{
if(opportunities.StageName == 'Prospecting')
{
newWrap.stage1.add(opportunities);
}
if(opportunities.StageName == 'Qualification')
{
newWrap.stage2.add(opportunities);
}
}
return newWrap;
}
}
Wrapper class:
public class opportunityWrapper {
@auraEnabled
public list<Opportunity> stage1= new list<Opportunity>();
@auraEnabled
public list<Opportunity> stage2= new list<Opportunity>();
}
Right now, im getting error 'Cannot read property 'data' of undefined'. Can anyone please help me on how to display the wrapper data correctly in this LWC?