0

Here, I need what customerData return but when in print customerData i got undefined

  if( phone === "" ||  phone === null || phone === undefined ) {
         var customerData =  pool.query('SELECT * FROM customers WHERE licensenumber = $1 ',[Licensenumber], (error, result) => {
             var customerData = result.rows;
             if (error) {
                        throw error
                       }
                  res.status(200).json(result.rows)
                    }) 
           }
           else{ 
       var customerData =pool.query('SELECT * FROM customers WHERE licensenumber = $1 AND phone = $2'
                    [Licensenumber,phone], (error, result) => {
                        var customerData = result.rows;
                            if (error) {
                                   throw error
                                   }
                            res.status(200).json(result.rows)
                                
                             })
                          }
    console.log(customerData);
//// i need to check what customerData  return 
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
labkumar
  • 11
  • 3
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Arif Khan Oct 29 '21 at 14:42

1 Answers1

0

You need to await the query call so the customerData varaiable is set you can do it with async await. comment if it helps.

 if( phone === "" ||  phone === null || phone === undefined ) {
         var customerData =  pool.query('SELECT * FROM customers WHERE licensenumber = $1 ',[Licensenumber], (error, result) => {
             var customerData = result.rows;
             if (error) {
                        throw error
                       }
                  res.status(200).json(result.rows)
                    }) 
           }
           else{ 
       var customerData =  await pool.query('SELECT * FROM customers WHERE licensenumber = $1 AND phone = $2'
                    [Licensenumber,phone], (error, result) => {
                        var customerData = result.rows;
                            if (error) {
                                   throw error
                                   }
                            res.status(200).json(result.rows)
                                
                             })
                          }
    console.log(customerData);


just add async keyword at start of the parent function

abhi patil
  • 504
  • 2
  • 11