Cannot read property 'publish' of undefined
Im getting this error a lot. in my code i have a integration with the backend, so when i move something here it goes to the server and moves to everybody else in real time, so i send all the data from the prop "onCardMoveAcrossLanes" (fromLaneId, toLaneId, cardId, index) to the server and receive it in all users, the im trying to use this data to make a eventBus.publish of type 'MOVE_CARD', but when i do it throws the error of 'Cannot read property 'publish' of undefined'... am i doing something wrong or is the eventbus no initializing properly? im using react hooks... here is a snippet of my code:
//eventbus define let eventBus = undefined
const setEventBus = (handle) => { eventBus = handle }
async function handleSocket(res) { ///recieves data from server
try {
eventBus.publish({ type: 'MOVE_CARD', fromLaneId: ${res.fromLaneId}, toLaneId: ${res.toLaneId}, cardId: ${res.cardId}, index: res.index })
} catch (error) {
console.error(error);
}
}
function updateLanes(data) { //function i use to update the lanes header(i show the number of cards there) this function works
try {
eventBus.publish({
type: 'UPDATE_LANES',
lanes: data.lanes.map((lane) => {
return { ...lane, label: ${lane.cards.length} }
})
})
} catch (error) {
console.error(error);
}
}
/******/
////JSX <Board onCardMoveAcrossLanes={onCardMove} data={data} eventBusHandle={setEventBus} onDataChange={updateLanes} /> //////////
the update lanes function works properly, but the one from the server doesnt.
@SrMatheus2000 I had the same issue as you. Got around it by using the React useState hook instead of way that the README suggests.
So instead of doing this:
let eventBus = undefined
const setEventBus = (handle) => {
eventBus = handle
}
I did this:
import React, { useState } from 'react'
const [eventBus, setEventBus] = useState(undefined)
Thank you @ACronje