May 22, 2023
0
Level 3
TypeError: Cannot read properties of undefined (reading 'id') in vue 2 in background worker
I am trying to run background worker in my vue app to all api check some status from database. But I getting error call "TypeError: Cannot read properties of undefined (reading 'id')" from that background worker. Here is my vue component method:
created() {
this.initWebWorker();
},
method: {
initWebWorker() {
const worker = new Worker(staticAlias);
worker.addEventListener('message', this.handleWorkerMessage);
worker.postMessage({ isAdmin: this.isAdmin, event: this.event, programId: this.programId })
this.worker = worker;
},
handleWorkerMessage(event) {
console.log(event);
},
},
beforeDestroy() {
this.worker.removeEventListener('message', this.handleWorkerMessage);
this.worker.terminate();
},
and here is my background worker file:
self.addEventListener('message', async function (evt) {
const { isAdmin, eventData } = evt.data;
console.log(evt);
if (!isAdmin) {
try {
const response = await fetch('check-event-status/' + eventData.id);
const data = await response.json();
if (localStorage.start_name === 'true' && data === 1) {
self.postMessage({ redirect: true, programId: eventData.programId });
}
} catch (error) {
console.error(error);
}
}
setTimeout(() => {
self.postMessage({ callApi: true });
}, 1000);
});
Anyone have any idea about this? Is there any other way to do this?
Please or to participate in this conversation.