I recently started to work with LWC, I want to bind a certain field from the controller and use it in my component
My LWC is on the Case object, I have a controller which returns an array of related email messages. What I want to do is to use the 'Incoming' field on my component:
import { LightningElement, api, track, wire } from 'lwc';
import getAllEmailsForRecord from '@salesforce/apex/getRelatedEmails.getAllEmailsForRecord';
export default class ShowRelatedRecords extends LightningElement {
@api recordId;
@track record;
@track error;
@wire(getAllEmailsForRecord, { caseId: '$recordId' })
emails;
get incoming() {
return this.emails.data.fields.Incoming.value;
}
}
Html:
<template for:each={emails.data} for:item="email">
<lightning-layout-item key={email.Id} size="12">
<div class="slds-p-around_x-small" >
<lightning-badge label={incoming}></lightning-badge>
</div>
....
Controller:
public without sharing class getRelatedEmails {
@AuraEnabled(Cacheable=true)
public static EmailMessage[] getAllEmailsForRecord(Id caseId){
return [SELECT Id,TextBody, IsExternallyVisible ,Subject,FromAddress,ToAddress,Incoming FROM EmailMessage WHERE ParentId = : caseId];
}
}
The result is that the LWC does not load and when I open the chrome debugger I see the following error:
Error: Uncaught (in promise) TypeError: Cannot read property 'Incoming' of undefined at i.get incoming [as incoming]
this.emails.data
? What does it return?