Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
How to store `returned value` of function from an sql query into a variable in Node js

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda