'why in javascript i cant create object in this function?

i have a problem with creating object inside my function i know its a void function but i don't need to return any data

let btnConnect = document.getElementById('connect')
let btnConnectedDrive = document.getElementById('ConnectedDrive')

function createPeer() {
    let myPeer = new Peer()
    console.log(myPeer.id)
    let socket = new WebSocket('ws://' + window.location.host + '/ws/myapp/room/' + game_id + '/');
    socket.onopen = () => socket.send(JSON.stringify(
        {
            'username': user,
            'PeerId': myPeer.id
        }
    ),)

}

btnConnect.onclick = () => createPeer();

now when i log myPeer.id its null and same on the server what do you think solution is ? hope you have a very nice time .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source