0

I have tried this code but couldn't find the error can you anyone help me out what issue in the code

Lightning DataTable.html file

<template>
    <lightning-card title="Lightning DataTable">
        <template if:true={oppList}>
        <lightning-datatable data={oppList} columns={column} key-field="Id">
        </lightning-datatable>
        </template>
        <template if:true={error}>
            {error}
        </template>
    </lightning-card>
</template>

Lightning DataTable.js file

    import { LightningElement ,api,track,wire} from 'lwc';
    import getOpportunities from '@salesforce/apex/GetOpportunities.getOpportunities';
   
    
    export default class LightningDataTable1 extends LightningElement {
        
    @track column = [{label: 'Name', fieldName: 'Name'},
                     {label: 'Amount', fieldName: 'Amount'},
                     {label: 'Stage', fieldName: 'StageName'}];
    
    @track error;
    @track oppList ;
    
    @wire(getOpportunities)
    wiredOpportunities({error,data}) {
        if (data) {
            this.oppList = data;
        } else if (error) {
            alert('error');
            this.error = error;
        }
    }
}

Apex Class

public with sharing class GetOpportunities {
    
        @AuraEnabled
        public static List<Opportunity> getOpportunities() {
            try {
                List<Opportunity> getopportunity = [Select Name,Amount,StageName from Opportunity Limit 5];
                return getopportunity;
        
                } catch (Exception e) {
                    throw new AuraHandledException(e.getMessage());
                }
            }
    }

I have attached the error message I am getting in org

enter image description here

Let me know in the comment section guys

0

1 Answer 1

2

I tried with your code in my dev org. I was getting :

Apex methods that are to be cached must be marked as @AuraEnabled(cacheable=true)

It was coming since you were using wire function to call the apex method but without (cacheable=true)

After adding it in the apex method, it fixed the issue.

public with sharing class GetOpportunities {
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getOpportunities() {
        try {
            List<Opportunity> getopportunity = [Select Name,Amount,StageName from Opportunity Limit 5];
            return getopportunity;
    
            } catch (Exception e) {
                throw new AuraHandledException(e.getMessage());
            }
        }
}

enter image description here

2
  • Thank you buddy It is working fine now for me also
    – Arjun
    Commented Jan 4, 2023 at 5:41
  • Great to know, You can mark it accepted since it worked. Commented Jan 4, 2023 at 6:05

You must log in to answer this question.

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