Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!