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

223
Vistas
Cannot change variable values from a callback scope

I designed a function that connects to MYSQL and calls the callback parameter with the 'connection' object. I'm calling this function(lets call it A) inside another function (lets call it B), and, in the callback I'm sending to function A, I'm trying to modify a value from function B, but it doesn't work. Simply the value remains the same.

The code:

Function A:

function databaseConnection(callback) {
    // MYSQL Connection Details
    const connectionDetails = {
        host: 'localhost',
        user: 'root',
        password: 'Mobileapp!?',
        database: 'social-app'
    }
    //

    // Configuring the connection
    const connection = mysql.createConnection(connectionDetails);
    connection.connect();
    //
    // Calling the user callback
    callback(connection);
    //

    // Ending database connection
    connection.end();
    //
}

Function B:


function isLoginDataValid(username, password) {
    let valid = true;

    // Starts the database connection and execute the given callback with the supplied object 'connection'
    databaseConnection(connection => {
        connection.query(`SELECT id from users where username = '${username}' and password = '${password}'`, (error, results, fields) => {
            if (!error && !(results.length))
                valid = false;
        })
    })
    //
    return valid;
}

Thank you!.

about 4 years ago · Juan Pablo Isaza
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