Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
Javascript How to use Async/Await with a mysql

I'm trying to query my mysql database for a value using Async/Await but my constant 'map' always returns undefined, I am not too sure why

const get_map = () =>{
    db.query('SELECT game,game2 FROM battles WHERE player1=? OR player2=?',[result.char,result.char],(err,results)=>{
        if(err){
            return(err)
        }
        if(!results[0]){
            return('No such battle?')
        }
        console.log(results[0])
        return(results[0])
    })
}
const proccessTurn = async ()=>{
    const map = await get_map()
    console.log(map)
    let game = JSON.parse(map.game)
    let game2 = JSON.parse(map.game2)
    const char = result.char
    const playerTurn = game2.turn[0]
}

The console doesn't even log

console.log(results[0])

This line so why does the await function resolve before it actually returns something? Shouldn't it wait for the returns?

Thank you for the help

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

 const get =async() => {  await prisma.post.findMany({ where: { title: { contains: 'cookies', },}, include: { author: true, // Return all fields },
})}

about 4 years ago · Juan Pablo Isaza Relatório

0

The third parameter to db.query() is a callback. If you're returning something in the callback, it's actually returned to the function itself (db.query in this case). It's not returning anything from get_map() . So return a new get_map promise like this,

 const get_map = () =>{ 
 return new Promise((resolve, reject) => { 
 db.query('SELECT game,game2 FROM battles WHERE player1=? OR player2=?',[result.char,result.char],(err,results)=>{ 
 if(err){ reject(err) } 
 if(!results[0]){ reject('No such battle?') } 
 console.log(results[0]) 
 resolve(results[0])
 }); 
 } 
}
const proccessTurn = async ()=>{ 
 const map = await get_map() 
 console.log(map) 
 let game = JSON.parse(map.game) 
 let game2 = JSON.parse(map.game2) 
 const char = result.char 
 const playerTurn = game2.turn[0] 
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda