.JS file
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ApexWireMethodToFunction extends LightningElement {
contacts;
error;
@wire(getContactList)
wiredContacts({ error, data }) {
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
}
HTML file:
<template>
<lightning-card
title="ApexWireMethodToFunction"
icon-name="custom:custom63"
>
<div class="slds-var-m-around_medium">
<template if:true={contacts}>
<template for:each={contacts} for:item="contact">
<p key={contact.Id}>{contact.Name}</p>
</template>
</template>
<template if:true={error}>
<c-error-panel errors={error}></c-error-panel>
</template>
</div>
<c-view-source source="lwc/apexWireMethodToFunction" slot="footer">
Call an Apex method that retrieves a list of records using @wire. In
this recipe, the Apex method is @wired to a component function.
</c-view-source>
</lightning-card>
</template>
APEX CLASS- CONTACTCONTROLLER
@AuraEnabled(cacheable=true)
public static List<Contact> getContactList() {
return [
SELECT
Id,
Name,
FirstName,
LastName,
MailingAddress
FROM Contact
WHERE Name != NULL
WITH SECURITY_ENFORCED
LIMIT 10
];
}
}
This is actually a code copy pasted from GitHub: 'LWC Recipes'--apexWireMethodToFunction LWC component.
But in HTML file, I have made small change. I have removed the line which has the reference for error-panel component and rest of the code is the same.
When I tried to deploy this code in my system I am getting the attached image as output. I am not sure as to why am I getting output as some undefined data.
Please help.