Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

320
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda