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!");
}
})
})