I tried to use async and promises in order to fix this error. But still I am not able to fix it.There are two files index.js
and db.js
in backend.
db.js
is the file which has the class with all the CRUD type methods in it.
index.js
is using that class object and sending the result to frontend.
Here is the error part in the code.
Inside index.js
socket.on("login", (User) => {
console.log(User);
let usersLoading = db.sign_in(User);
console.log("UserLoading is... \n", usersLoading);
socket.emit("UsersLoading", usersLoading);
});
Inside db.js
async sign_in(user) { // this is the async method
let fquery =
"select * from Users where (Username = '" +
user.Username +
"'and Password = '" +
user.Password +
"')";
let response = [];
await this.con.query(fquery, function (err, result) {
if (err) {
console.log(err);
} else {
if (result.length == 0) {
response.push({
Condition: "empty",
});
} else {
response.push({
Condition: "notempty",
});
response.push(result[0]);
}
}
});
return response; // I am trying to send the query response back to index.js file.
}
User value in inxed.js is :
{
Username : "xyz",
Password : "abc"
}
Actually the query part is working correctly but it is not able to store the value in 'response' variable. I also tried to do this after reading this and this page but unfortunately I was not able to solve this issue.