Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

express mysql to js variable gets undefined

hi I want to assign result of MYSQL query to JS variable to use in serval times but gets back empty array what is problem?
my code

app.get('/api/get/db',async(req,res)=>{

async function test() {
new Promise((resolve,reject)=>{
  con.query("SELECT * FROM `temp`",(err,result)=>{
   resolve(result)
  })
 })
}
data =await test()

res.status(200).json({data})
})

i tried to return but not working

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

app.get('/api/get/db',async(req,res)=>{

async function test() {
//here you have to add **return** keyword
return new Promise((resolve,reject)=>{
  con.query("SELECT * FROM `temp`",(err,result)=>{
   resolve(result)
  })
 })
}
data =await test()

res.status(200).json({data})
})

you have to add return keyword before promise object to return

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading