How to resolve the error - cannot read property 'play' of undefined?
I'm getting the above error in the 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 {
@AuraEnabled(cacheable=true)
public static void sendEmailToController(String toSend, string firstName, string lastName, Integer phoneNumber){
try{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toSend};
mail.setToAddresses(toAddresses);
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';
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}catch(Exception ex){
throw new AuraHandledException(ex.getMessage());
}
}
}
I don't find any problem with the above code but I'm still getting the below error. Any help would be appreciated.
error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'body')
To resolve the error - cannot read property 'play' of undefined, you must understand that - 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)