I want to subscribe to events in a community using the CometD library. Unfortunately when loading the script I get following error: "Error: Cannot read properties of undefined (reading 'cometd')"
This is my code:
import { LightningElement, api, wire, track } from 'lwc';
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
import triggerSomeEventBoy from '@salesforce/apex/AccountController.triggerSomeEventBoy';
import getAllActiveAccounts from '@salesforce/apex/AccountController.getAllActiveAccounts';
import getSessionId from '@salesforce/apex/AccountController.getSessionId';
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from '@salesforce/resourceUrl/CometD';
export default class GerkesComponent extends LightningElement {
libInitialized = false;
@track sessionId;
@track error;
sessionId = "";
@wire(getSessionId)
wiredSessionId(result) {
var error = result.error;
var data = result.data;
if(data) {
console.log('sesssion id: ' + data);
this.sessionId = data;
this.error = undefined;
loadScript(this, cometdlwc).then(() => {
console.log('script loaded yippie!');
this.initializecometd();
}).catch((error) => {
console.log('found error:');
console.log(error);
});
} else if (error) {
console.log(error);
this.error = error;
this.sessionId = undefined;
}
}
}
Does anyone know why this error occurs and how to solve it?