DEV Community

liu550
liu550

Posted on

Uncaught TypeError: Cannot read property "state" of undefined

Hi,

I am wondering why the console reports the error at line 36. Thanks for your help.

Alt Text

Top comments (6)

Collapse
 
krutoo profile image
Dmitry Petrov • Edited

You need to bind your callback function (at line 30) or use arrow function

Collapse
 
liu550 profile image
liu550

Thank you so much for pointing this out! But I am still not sure how to bind the createSession method and the parameter function that takes in session and error (or use arrow function for these two), since they are methods of the OpenTok class. This part of the code is provided by the OpenTok platform.

Collapse
 
krutoo profile image
Dmitry Petrov

You need to bind exactly callback function but not the "createSession" method of "OpenTok" instance.

Maybe I didn’t fully understand the problem, but why can't you do that?

// line 30
opentok.createSession({ mediaMode: 'router' }, (error, session) => { /* ... */ })
Thread Thread
 
liu550 profile image
liu550

Thanks! This fixes the error. Still have another question: console.log(this.state.sessionID) at line 17 and 19 output an empty string (the initial value), and this is problematic because the value inside the doc() at line 20 cannot be empty. However, I later added console.log(this.state.sessionID) after line 36, which output a non-empty sessionID string, suggesting that the generateSession function did successfully modify this.state.sessionID. What makes the change in this.state.sessionID not passed to the handleSubmit function?

Collapse
 
tomekbuszewski profile image
Tomasz Buszewski

Dude! Do not mutate the state! Use setState (more info: reactjs.org/docs/state-and-lifecyc...).

Collapse
 
liu550 profile image
liu550

Thanks man!