简体   繁体   中英

Query not being executed with async await in Node js

I am trying to make a GET request in my node js application, but for some reason its not working with async await. It works when i use the callback chain but when i try to execute the same query with async await its giving me an error:

TypeError: Converting circular structure to JSON at JSON.stringify ()

my code:

router.get("/getuser", async(req,res)=>{
const emailexists= 'select * from users'
try{
    const results=await connection.query(emailexists)
    res.send(results)
}catch(err){
    console.log(err)
}
})

I am using mysql as database. Can anyone please help?

The mysql library you are using does not support promises. So, connection.query(emailexists) does not return a promise and thus using await with it does not do anything useful and does not wait for the result.

You can either manually promisify the specific functions you want to use, use a third party promise wrapper for mysql or you can use the newer mysql2/promise library which has built-in promise support. See that link for programming examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM