Cannot read property 'publishAudio' of undefined error when adding mute to openvidu-js-node

Hello,

I’m trying to add simple mute functionality to openvidu-js-node tutorial. I’m using get-a-room tutorial snippets.

Here is what I did.

I added following code into the index.html (above the leave session button):

<input class="btn btn-large btn-danger" type="button" id="buttonMuteVideo" onmouseup="muteVideo();" value="Wideo">
<input class="btn btn-large btn-danger" type="button" id="buttonMuteAudio" onmouseup="muteAudio();" value="Audio">

In app.js I added following code in to the top of the file:

var publisher;
var audioEnabled = true;
var videoEnabled = true;

& following code under the joinSession() function:

function muteAudio() {
audioEnabled = !audioEnabled;
publisher.publishAudio(audioEnabled);
}

function muteVideo() {
videoEnabled = !videoEnabled;
publisher.publishVideo(videoEnabled);
}

When I use these buttons I’m getting following errors in console:

Uncaught TypeError: Cannot read property 'publishVideo' of undefined
at muteVideo (app.js:133)
at HTMLInputElement.onmouseup ((index):123)
muteVideo @ app.js:133
onmouseup @ (index):123
app.js:120 Uncaught TypeError: Cannot read property 'publishAudio' of undefined
at muteAudio (app.js:120)
at HTMLInputElement.onmouseup ((index):124)

Can you help me please to figure out what I’m doing wrong?

Your Publisher object is undefined

Thanks, I got that but I fail to understand what I can do in the code to fix this. Any advice?

You just have to initialize your “publisher” variable first. Assign the proper value to “publisher” before calling any method upon that object or you will get that error. If the initialization of the publisher is asynchronous, be sure to access the object after the initialization method has successfully returned.

I don’t really know what else to say to you. This is basic JS stuff: accesing a method or property for an undefined object will throw an error. This is similar to a NullPointerException in other languages.

Yup, was my error in the code. Thanks for your help.