EDIT: I originally left out a part of my code which I thought was irrelevant to the question but on further investigation realized it was the source of the issue. Adding it in and then will also add the answer.
I was actually calling handleReleaseHold()
instead of directly calling doReleaseHoldRequest()
as I mentioned
I am trying to display a toast notification in Lightning Experience with the results of an API callout from my JS file. I'm not getting an error, but no toast is displaying. Makes me think that it’s an issue of event bubbling/listening from within the JS fetch method, but I haven’t been able to figure it out.
JS file:
import { LightningElement, track, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class HoldModal extends LightningElement {
@track accountId = '001P000001OUxqZIAT';
@track msgToDisplay = 'default';
/// Added in this section - start
handleReleaseHold(){
this.doReleaseHoldRequest();
this.doCloseModal();
}
doCloseModal() {
const buttonPressed = "save";
const closeModalEvent = new CustomEvent('modalClosed', { detail: buttonPressed });
this.dispatchEvent(closeModalEvent);
}
/// Added in this section - end
doDisplayToastMsg(variant, title, message) {
const toastEvt = new ShowToastEvent({
variant: variant,
title: title,
message: message,
});
this.dispatchEvent(toastEvt);
}
doReleaseHoldRequest(){
const url = 'https://www.url.com';
const params = {
method: 'POST',
headers: {
"Content-Type" : "application/json",
"Authorization" : "Token token=xxxxxxxxxxxxxxxxxxx"
},
body: '{"key": "value"}'
};
const thisReference = this; // different this scope when called from within subfunctions, so setting variable here to use correct this
fetch(url, params)
.then((response) => response.json())
.then(function(data){
if(data.message){
// handle later
}
else if(data.errors){
// 1. // console.log('thisReference.accountId: ' + thisReference.accountId);
// 3. // thisReference.doDisplayToastMsg('error', 'within fetch', 'this is the error');
console.log('thisReference.msgToDisplay - before: ' + thisReference.msgToDisplay);
// 4. // thisReference.msgToDisplay = 'MESSAGE INSIDE FETCH';
console.log('thisReference.msgToDisplay - after: ' + thisReference.msgToDisplay);
// 4.a. // thisReference.doDisplayToastMsg('error','within fetch',thisReference.msgToDisplay);
/* 5. const toastEvt = new ShowToastEvent({
variant: 'error',
title: 'title',
message: 'message',
});
thisReference.dispatchEvent(toastEvt);
*/
}
})
.then(function(){
// 6. //thisReference.doDisplayToastMsg('error','last then before catch' ,thisReference.msgToDisplay);
})
.catch(function(error){
console.log('doReleaseHoldRequest error: ' + error);
})
// 2. // thisReference.doDisplayToastMsg('error','after fetch', 'this is the error');
// 4.b. // thisReference.doDisplayToastMsg('error','after fetch',this.msgToDisplay);
}
}
When I call doReleaseHoldRequest()
I do get into the
else if(data.errors){
block fine which is where I'm trying to call the toast
Things I’ve tried (numbers associated with commented out sections in above code):
Confirmed that
thisReference.accountId
prints what I expect so seems that correct this context is being usedConfirmed that firing a toast message with my
doDisplayToastMsg()
helper method AFTER thefetch()
method works => the toast displays correctly as expected- Tried firing toast message with
doDisplayToastMsg()
helper method WITHIN the fetch method => no toast is displayed Tried populating
@track msgToDisplay
with the results from the fetch method and then calling thedoDisplayToastMsg()
a. right afterwards within the
fetch()
method => no toast is displayedb. after the fetch method => toast is displayed but not with updated message content (which makes sense, because the toast is probably being displayed before the fetch method is complete)
Tried creating and dispatching a toast event without using my helper method
doDisplayToastMsg()
(which really shouldn't make a difference, but just in case) => no difference, toast still did not display- Added another
.then()
and called thedoDisplayToastMsg()
there with a reference to the message content variable => no toast is displayed