-1

i am trying to get data from MySQL database using node.js and i need to use it on other modules so i need to send query result it seem so easy but i get stuck :

i get data using this calss and funcation:

class user{
user(){}
static async getUser(email: string) {
        let Finalresult = await DataBase.query("SELECT * FROM childcare.user where email =?", [email], function (err, result, fields) {
            if (err) return err;
            else
                return result;

        });

        return Finalresult;

    } }
module.exports = user;

i want to use data in that function and i expect to get the result of query

const userModule = require("../../modules/user");
static async login(req: Request, res: Response) {
    const { email, password } = req.body;
    const user = await userModule.getUser(email);
    console.log('user is ', user);)}

But i get user is *<ref 1> Query

  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – derpirscher Aug 27 '23 at 16:51

1 Answers1

1

You can't use then/catch and async/await together. You need to remove the callback part.

class User{
user(){}
async getUser(email: string) {
        try {
           let finalResult = await DataBase.query("SELECT * FROM childcare.user where email =?", [email])
           return finalResult;
        } catch(e) {
           console.log(e);
        }
        

    } }
module.exports = new User();

In your calling module:

const userModule = require("../../modules/user");
static async login(req: Request, res: Response) {
    const { email, password } = req.body;
    const user = await userModule.getUser(email);
    console.log('user is ', user)
  }

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35