New to LWC and looking to call an Apex method and get data to display. It seems I'm missing something here. I get nothing back and don't see any error. Is there anything I'm missing?
Javascript file:
import { LightningElement, api, wire, track } from 'lwc';
import getMyList from '@salesforce/apex/ListDetailsClass.getMyLists';
export default class ApexWireMethodToProperty extends LightningElement {
@api listGroup;
@api recordId;
@api objectApiName
@track mylists;
@track error;
connectedCallback() {
this.recordId = this.recordId;
this.objectApiName = this.objectApiName;
}
@wire(getMyList, { groupLabel: '$listGroup', objectName: '$objectApiName', recordId: '$recordId' })
wiredFlows({ error, data }) {
if (data) {
this.mylists = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.mylists = undefined;
}
}
}
HTML file:
<template>
<template for:each={mylists} for:item='listItem'>
<div key={listItem.Name} class="slds-col slds-small-size_12-of-12 slds-medium-size_6-of-12">
123
</div>
</template>
</template>
Apex Class method:
@AuraEnabled(cacheable=true)
public static string getMyLists(string groupLabel, String objectName, Id recordId) {
try {
List<Financials__c> financials = [SELECT Id, Name, Dependent_On__c, Record_Type_Developer_Name__c, FROM Financials__c WHERE Label__c = :groupLabel ORDER BY Order_Number__c];
System.debug('my return value: ' + JSON.serialize(financials));
return JSON.serialize(financials);
} catch (Exception ex) {
System.debug('exception:' + ex);
return '';
}
}
This is part of migration from Aura to LWC so Apex works with Aura component and it is tested. Apex class is called successfully by LWC as well and problem seem to be with consuming data coming in from Apex
@AuraEnabled
(this method: ListDetailsClass.getMyLists)? Also, what do you see in the network tab in Chrome? Is there an apex action?cacheable=true
which means that LDS will only request it once. try sayingcacheable=false
. In theory LDS is meant to use parameters to distinguish requests but i've not seen that working reliably.JSON.serialize
This is a biggest NO NO in all of web development with a framework, including SF apex. Just return your objects directly... What is happening atm is yourmylists
is holding string json which, of course, is not iterable in thefor:each
. To confirm just add{mylists}
somewhere in html file