mardi 25 juin 2019

Cannot send variable name from one node module to another to use it as a room-name for chat application using socket.io

I am able to get the room name from user but as I use module.exports in another file to retrieve it shows undefined . This might be because it is asynchronous.

//roomcheck.js

var nsp = io.of("/gameroom");
nsp.on('connection', (socket) => {
socket.on('check', (data) => {
    if (rm.includes(data)) {
        roomname = data;
        console.log(roomname);
        console.log("Room exist!!!");
        socket.emit('success', 'room already made');

});

        module.exports=roomname;

//userpage.js

var r=require('roomcheck.js');
console.log(r.roomname);

expected- my output- undefined




Aucun commentaire:

Enregistrer un commentaire