Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!