function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srkSFsrkSF 

sql query fetching data and does not display records in lwc

Hi All,
UpcomingEvents query is fetching records  properly using Developer Console.But I am seeing just blank white card(LWC) only when I am clicking Home page.
Please help me!!!!!
User-added imageUser-added image
Apex Class:
--------------------------------------------------------------
public without sharing class EventListLWCService {
    @AuraEnabled(cacheable=true)
    public static List<Event__c> fetchUpcomingEvents(){
        List<Event__c> eventList=new List<Event__c>();
        try {
            eventList= [SELECT Id,Name,Name__c,CreatedDate,Event_Detail__c,Location__c,Location__r.Name,Event_Organizer__c,
                        Start_DateTime__c,End_Date_Time__c,Event_Organizer__r.Name,Event_Organizer__r.Email__c,Recurring__c,
                        Max_Seats__c,Status__c,Remaining_Seats__c,PeopleAttending__c
                        FROM Event__c
                        WHERE Start_DateTime__c >= Today AND Live__c = true
                        WITH SECURITY_ENFORCED
                        ];
           
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return eventList;
    }
    @AuraEnabled(cacheable=true)
    public static List<Event__c> fetchPastEvents(){
        List<Event__c> eventList=new List<Event__c>();
        try {
            eventList= [SELECT Id,Name,Name__c,CreatedDate,Event_Detail__c,Location__c,Location__r.Name,Event_Organizer__c,
            Start_DateTime__c,End_Date_Time__c,Event_Organizer__r.Name,Event_Organizer__r.Email__c,Recurring__c,
            Max_Seats__c,Status__c,Remaining_Seats__c,PeopleAttending__c
            FROM Event__c
            WHERE Start_DateTime__c <= Today AND Live__c = true
            WITH SECURITY_ENFORCED
         ];
           
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return eventList;
    }
   
}

HTML:
-----------------------------------------------
<template>
    <lightning-card variant="Narrow">
        <!--Upcoming Events-->
        <div class="slds-text-heading_large">Upcoming Events</div>
        <hr/>
        <lightning-layout multiple-rows>
            <lightning-layout-item size="12" padding="around-small" small-device-size="12" medium-device-size="6" large-device-size="4">
                <div class="slds-var-p-around_large">1
                </div>
            </lightning-layout-item>
            <lightning-layout-item  size="12" padding="around-small" small-device-size="12" medium-device-size="6" large-device-size="4">
                <div class="slds-var-p-around_large">2
                </div>
            </lightning-layout-item>
            <lightning-layout-item  size="12" padding="around-small" small-device-size="12" medium-device-size="6" large-device-size="4">
                <div class="slds-var-p-around_large">3
                </div>
            </lightning-layout-item>
            <template if:true={upComingEvents} for:each={upComingEvents} for:item="event" for:index="index">
            <lightning-layout-item key={event.Id} size="12" padding="around-small" small-device-size="12" medium-device-size="6" large-device-size="4">
                <div class="slds-var-p-around_large">
                <p>{event.Name__c}</p>
                <p>{event.Start_DateTime__c}</p>
                <p><lightning-formatted-rich-text value={event.Event_Detail__c}></lightning-formatted-rich-text></p>
                </div>
            </lightning-layout-item>
            </template>
        </lightning-layout>
        <!--Past Events-->
        <div class="slds-text-heading_large">Past Events</div>
        <hr/>
        <lightning-layout multiple-rows>
            <template if:true={pastEvents} for:each={pastEvents} for:item="event" for:index="index">
            <lightning-layout-item key={event.Id} size="12" padding="around-small" small-device-size="12" medium-device-size="6" large-device-size="4">
                <div class="slds-var-p-around_large">
                    <p>{event.Name__c}</p>
                    <p>{event.Start_DateTime__c}</p>
                    <p><lightning-formatted-rich-text value={event.Event_Detail__c}></lightning-formatted-rich-text></p>
            </div>
            </lightning-layout-item>
            </template>
        </lightning-layout>
    </lightning-card>
</template>
JavaScript file:
-----------------------------------------------------------------------
import { LightningElement, wire } from 'lwc';
import fetchUpcomingEvents from '@salesforce/apex/EventListLWCService.fetchUpcomingEvents';
import fetchPastEvents from '@salesforce/apex/EventListLWCService.fetchPastEvents';
export default class EventListComponent extends LightningElement {
    upcomingEvents;
    pastEvents;
    __errors;
    isSpinner=false;
    @wire(fetchUpcomingEvents)
    wiredUpcomingEventsData({ error, data }) {
      if (data) {
        console.log('Data', data);
        this.upcomingEvents=data;
      } else if (error) {
        console.error('Error:', error);
        this.upcomingEvents=undefined;
        this.__errors=error;
      }
    }
    @wire(fetchPastEvents)
    wiredPastEventsData({ error, data }) {
      if (data) {
        console.log('Data', data);
        this.fetchPastEvents=data;
      } else if (error) {
        console.error('Error:', error);
        this.fetchPastEvents=undefined;
        this.__errors=error;
      }
    }
}
Please let me know where i am doing wrong?

Thanks
 
ravi soniravi soni
hi srkSF,
I reviewed you code and code is looking good but I think data is not coming properly. Do one thing copy your query and just paste it in query editor and check data is coming or not. If data is coming then check console.log in your js file. I think error is coming.
you can add error in error part.

don't forget to mark it as best answer if you satisfiedwith my answer.
Thank you
srkSFsrkSF
Hi Veer soni,
Thanks for replying.
When I execute the query in Developer Console,3 Records are fetched in Developer Console but only if I dont include SECURITY_ENFORCED clause in actual query.
Data is coming and showing records in Browser Console.There is no error coming in Console.
there is SECURITY_ENFORCED clause in upComingEvents SOQL query,that means user should have permissions
to view the Event Object records.I will check in that direction.

User-added image