react-trello icon indicating copy to clipboard operation
react-trello copied to clipboard

Cannot read property 'publish' of undefined

Open SrMatheus2000 opened this issue 5 years ago • 1 comments

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 avatar Apr 16 '20 12:04 SrMatheus2000

@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)

ACronje avatar Jul 08 '20 23:07 ACronje

Thank you @ACronje

SrMatheus2000 avatar Oct 11 '23 13:10 SrMatheus2000