I want to create login system using node.js and Redis (Redis package) but every time I compare input data with database data I get a confusing result. This is the first method:
const Redis = require("redis");
const client = Redis.createClient();
exports.userLogin = (username, password, errMsg, successMsg, res) => {
client.keys("user*", (err, replies) => {
let isFound = false;
for (let i = 0; i < replies.length; i++) {
client.hgetall(replies[i], (err, value) => {
if (value.user === username && value.password === password) {
isFound = true;
}
});
}
console.log(isFound);
if (isFound) {
successMsg(username, res);
} else {
errMsg(res);
}
})
)}
I keep getting false even if login and password are ok plus It doesn't execute errMsg function
Second example:
exports.userLogin = (username, password, errMsg, successMsg, res) => {
client.keys("user*", (err, replies) => {
for (let i = 0; i < replies.length; i++) {
client.hgetall(replies[i], (err, value) => {
if (value.user === username && value.password === password) {
successMsg(username, res);
} else {
console.log("length: ", replies.length - 1);
console.log("i: ", i);
if(replies[i] === replies.length - 1) {
errMsg(res);
} else {
console.log("else statement: ", replies[i] === replies.length - 1);
return true;
}
}
});
}
})
)}
It works if password and login are ok but It doesn't if they are not correct because the page is loading something and doesn't execute the errMsg function. Do you have any ideas?
