0

So, here's my code:

const LogInform = document.getElementById('login');
const UsernameInput = document.getElementById('username');
const PasswordInput = document.getElementById('password');

console.log(LogInform);

LogInform.addEventListener('submit', (event) => {
  event.preventDefault();
  const username = UsernameInput.value;
  const verifyusername = username;
  console.log(verifyusername);
  const password = PasswordInput.value;
  while (opened_websocket == false) {
    console.log("Waiting for connection");
  }
  if (opened_websocket == true) {
    ws.send(JSON.stringify([null, password, "connect", "connect", username]));
    console.log("Sent login details to the server!");
  }
  UsernameInput.value = '';
  PasswordInput.value = '';
});

If I try to call the variable: verifyusername or username, it outputs undefined. I've looked at some articles, and nothing seems to fix it. I've tried:

  • Placing var verifyusername; at the start of my program
  • Calling this.verifyusername
  • Assigning it to a seperate variable called verifyusername

Please note: The event listener does execute, before I call the variable

THe code when I call the variable:

websocket.on("request", request=> {

  connection = request.accept(null, request.origin);
  connection.on("open", () => console.log("A new WebSocket request has been opened."))
  connection.on("close", () => {
    console.log("A WebSocket request has been closed.");
  })
  connection.on("message", message => {
    console.log("EVENT----------------------------------------");
    array = JSON.parse(message.utf8Data);
    console.log("Recived message:");
    console.log("Message IP Origin(s): "+request.remoteAddresses);
    console.log("Message Data: "+array);
    console.log("==============================================");
    /* 
    ARRAY SYNTAX:
    [id, password, channel, command, value]
    "ID": An identifier to distiguish clients.
    "password": Confirms that the user has sent the message, and that no one impersonated them.
    "channel": Confirms which channel they are in (eg: chat, game, ext...) {DEFULT: "chat"}
    "command": A command to be executed. eg(X-Position)
    "msg": THe command value (message).
-------------------------------------------------------------
    EXAMPLE:
    [123456789, "Passw0rd", "chat", "message", "Hi!"]
    */
    id = array[0];
    password = array[1];
    channel = array[2];
    command = array[3];
    msg = array[4];
    if ((command == "connect") && (channel == "connect")) {
      console.log("New user login. A user is trying to get the credentials. This user is unverified, and this may throw a 'Undefined user' error.");
      fs.readFile('all-Ids.json', function(err, data) {
        ALL = JSON.parse(data);
        e = 0;
        while (typeof id === "undefined" || id === null) {
          if (users[ALL[e]].name === msg) {
            id = ALL[e];
          } else {
          e = e+1;
          }
        }
        console.log(id);
        if (typeof id !== "undefined" && id !== null) {
          console.log("ID has been identified!");
          if (users[id].password === password) {
            console.log("Password accepted:");
            connection.send(JSON.stringify([id, "verified", msg]));
            console.log("SENT TO CLIENT: "+JSON.stringify([id, "verified", msg]));
          }
        }
      });
    }
    user = users[id];
    if (typeof user !== 'undefined') {
      check();
    } else {
      console.log("An undefined user has tried to send a packet!");
    }
  })
})
Edvinas55
  • 11
  • 6
  • But how do I make the variable accessible globally? – Edvinas55 Mar 02 '23 at 16:35
  • 2
    Why should it be global at all? Aren't you using it *inside* the listener with `ws.send(JSON.stringify([null, password, "connect", "connect", username]));`? Where exactly are you seeing an `undefined` ? – VLAZ Mar 02 '23 at 16:38
  • Can you post your HTML for `document.getElementById('username')`. Possibly you are referencing the element wrong? – khollenbeck Mar 02 '23 at 16:39
  • No. When I call the variable **OUTSIDE** the event listener, it outputs undefined. The `console.log(verifyusername);` inside the event listener outputs the username. – Edvinas55 Mar 02 '23 at 16:39
  • 1
    *Placing var verifyusername; at the start of my program* - this will work, but only if you remove the *inner scoped* variable declaration - ie `const verifyusername =` -> `verifyusername =` – freedomn-m Mar 02 '23 at 16:40
  • All of the HTML and javascript are checked, and it all works. Hope this helps! – Edvinas55 Mar 02 '23 at 16:40
  • 1
    Please include the code where you call it *outside* the event listener. There's some confusion as you've not included this in the question so all we can see is the one *inside* the event listener. However, it would be irrelevant as, as provided, your `username` variable(s) are scoped inside the event handler, so do not exist outside (as noted above). – freedomn-m Mar 02 '23 at 16:41
  • Okay. I'll update it – Edvinas55 Mar 02 '23 at 16:42

0 Answers0