I'm getting above error on below code.

@api guestFirstName; //getting these from parent component
@api guestLastName;
@api guestPhoneNumber;
@api guestCodeFromGuestMaster;
@api guestEmailFromGuestMaster;

//LWC code

   sendEmailAfterEvent() {
    const recordInput = { toSend: this.guestEmailFromGuestMaster, firstName: this.guestFirstName, lastName: this.guestLastName, phoneNumber: this.guestPhoneNumber }  //send parameters
    sendEmailToController(recordInput).then(response => {
        console.log('Email has been sent...!');
    }).catch(error => {
        console.log('facing error in sending email ',error.message.body); //If there is an error on response


//APEX controller code

public with sharing class ControllerLwcExample {
public static void sendEmailToController(String toSend, string firstName, string lastName, Integer phoneNumber){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {toSend};
        mail.setSubject('Booking Confirmation');
        String body = 'Dear ' + firstName + ', ';
        body += 'Your booking has been confirmed.';
        body += 'Please find the details below:';
        body += 'First Name: '+firstName;
        body += 'Last Name: '+lastName;
        body += 'Phone Number: '+phoneNumber;
        body += 'Regards';
        body += 'Hotel Hotel Hotel';
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }catch(Exception ex){
        throw new AuraHandledException(ex.getMessage());


I don't find any problem with above code but still getting below error. Any help would be appreciated.

error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'body')

  • Wondering if the cause of the actual error is mail.setHtmlBody(body); in the Apex. I see that OP is not setting HTML, rather plain text. Perhaps it should be mail.setPlainTextBody(body); Mar 26, 2022 at 8:34

1 Answer 1


You likely have two errors.

The first is that you are trying to send an email from a cacheable method (which you can't do, as a cacheable cannot perform DML).

This will be causing an error. Change:

@AuraEnabled(cacheable=true) to @AuraEnabled

Then, this error is getting to the LWC component, and this is where you are getting the body issue. Javascript exceptions don't have a body property on the message

Change: error.message.body to error.message

(Either that or try Phil's comment) (error.body.message)

  • Is it not error.body.message? Mar 26, 2022 at 8:04
  • Perhaps... I've never seen a javascript exception have a body property at all. Mar 26, 2022 at 8:05
  • I'm wondering if the framework does it then, I just checked one of my projects and thats the value I have used Mar 26, 2022 at 8:08
  • 2
    note case on json it would be console.log(JSON.stringify(error)); if you go that route Mar 26, 2022 at 8:27
  • 2
    Looks like reading the error is a real pita look here: salesforce.stackexchange.com/questions/325258/… - thanks @PhilHawthorn for the JSON thing... getting late here ;) Mar 26, 2022 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .