0

outputimage

.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.

1 Answer 1

0

My best bet would be your user profile doesn't have access to the apex class. Thus the component is displaying the error part. As we don't see the error component code, it's hard to help you more.

You could use the browser devtool, go to network tab and search for your apex method call and see what is the raw answer from the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.