Uncaught TypeError: Cannot read properties of undefined (reading ‘call’)

  Kiến thức lập trình

I was following a tutorial on youtube on create a video chat app with react by JavaScript Mastery (https://youtu.be/oxFr7we3LC8?si=BTWDF9OIQpVvbs2O). The person used create-react-app but I’m using vite. Every other part has been working except for when im making a call. This is what i get in the browser’s console:

Uncaught TypeError: Cannot read properties of undefined (reading 'call')
    at _Peer.Readable (simple-peer.js?v=4d304747:4300:14)
    at new Duplex (simple-peer.js?v=4d304747:3569:16)
    at new _Peer (simple-peer.js?v=4d304747:5309:9)
    at callUser (Context.jsx:61:18)
    at onClick (Options.jsx:45:32)
    at HTMLUnknownElement.callCallback2 (chunk-3GS3N7PG.js?v=5de4bc54:3674:22)
    at Object.invokeGuardedCallbackDev (chunk-3GS3N7PG.js?v=5de4bc54:3699:24)
    at invokeGuardedCallback (chunk-3GS3N7PG.js?v=5de4bc54:3733:39)
    at invokeGuardedCallbackAndCatchFirstError (chunk-3GS3N7PG.js?v=5de4bc54:3736:33)
    at executeDispatch (chunk-3GS3N7PG.js?v=5de4bc54:7016:11) 

The error comes about when im trying to create a new Peer object with simple-peer

This is the function to call a user:

 const callUser = (id) => {
    console.log('calling....')
    const peer = new Peer({ initiator: true, trickle: false, stream })
    console.log(peer)

    peer.on('signal', (data) => {
      socket.emit('callUser', { userToCall: id, signalData: data, from: me, name });
    });

    peer.on('stream', (currentStream) => {
      userVideo.current.srcObject = currentStream;
    });

    socket.on('callAccepted', (signal) => {
      setCallAccepted(true);

      peer.signal(signal);
    });

    connectionRef.current = peer;
  }

I checked several forums but did not find any problem related to mine and the one i found recommended me to download vite-plugin-node-polyfills it did not fix the problem but rather created a new problem

New contributor

Emmanuel Selase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT