Unable to display the data retrieved from Apex method in LWC

lightning-web-components

I'm passing a parameter(Id) from LWC to a method in Apex for getting a list of Opportunities, but for some reason I'm not able to getting the results from method.

HTML

<template>
    <lightning-card title="Opportunty List ">
        <div class="slds-p-around_xx-small">
            <lightning-datatable
                key-field="Id"
                data={lstOpps}
                columns={lstColumns}>
        </lightning-datatable>
        </div>
    </lightning-card>
</template>

JavaScript

import { LightningElement, track, api, wire } from 'lwc';
import fetchOpportunity from '@salesforce/apex/ExistingOppInLead.getExistingOpp';
const COLUMNS = [
    {
        label: 'NAME', fieldName: 'oppURL', type: 'url',
        typeAttributes: {
            label: {
                fieldName: 'Name'
            }
        }
    },
    {label: 'STAGE', fieldName: 'Stagename', type: 'text'},
    {label: 'ORDER DATE', fieldName: 'closedate', type: 'date'}
];

export default class ExistingOpportunities extends LightningElement {
    @track lstOpps;
    @api recordId;
    lstColumns = COLUMNS;
    
    connectedCallback(){
        fetchOpportunity({id: '$recordId'}).then(response => {
            this.lstOpps = response;
            if(this.lstOpps){
                this.lstOpps.forEach(item => item['oppURL'] = '/lightning/r/Opportunity/' +item['Id'] +'/view');
                
            }
        }).catch(error => {
            console.log('Error: ' +error);
        });
    }

}

Apex Method

public with sharing class ExistingOppInLead {
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getExistingOpp(Id id){
        String[] filters = new String[]{'Pre-Funnel','Recognition of Needs','Evaluation/Presentation of Solution',
        'Resolution of Concerns','Final Negotiation Phase','Dead/Cancelled', 'Deal Won',
        'Deal Lost'};
        System.debug('Id is '+id);
        String lemail = [SELECT ID, EMAIL FROM LEAD WHERE ID =: id AND CREATEDDATE >= LAST_N_DAYS:3 LIMIT 1 ].EMAIL;
        List<Opportunity> result = new List<Opportunity>([select id, Name, CloseDate, stageName from Opportunity where CloseDate >= LAST_N_YEARS:2
            AND StageName IN:filters AND Primary_Contact_Email__c =: lemail]);
        

        return result;
    }
}

Best Answer

Problem is below line of code

fetchOpportunity({id: '$recordId'}).then(response => {

Instead it must be

fetchOpportunity({id: this.recordId}).then(response => {

Another suggestion. Next time you ask questions, please post some error or what you have tried to solve the issue. That would help others to pin point the issue sooner.