Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

407
Vistas
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

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

As the mysql library doesn't support promises, you can use the promisify util function from Node.js like this:

const { promisify } = require('util');

const query = promisify(db.query).bind(db);

const get_map = async () => {
    try {
         const result = await query('SELECT game,game2 FROM battles WHERE player1=? OR player2=?',[result.char,result.char]);
         if(!results[0]){
             return 'No such battle?';
         }
         console.log(results[0])
         return results[0];
    }
    catch(e) {
         return e;
    }
}
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]
}
over 4 years ago · Santiago Trujillo Denunciar

0

The third parameter to db.query() is a callback. If you're returning anything in callback it's actually returned to the function itself, (db.query in this case). You're not returning anything from get_map(). So return a new promise from get_map 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]
    });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda