Tuesday, 14 March 2017

Parent scope variable can't be modified within child scope

I'm pretty new to Javascript and node.js and I'm having some trouble getting this to work. As far as I know, this should produce the expected result but I'm having some rather weird outputs (cf. comments).

function getHistoryFromDb() {
    var rows=1;
    pool.getConnection(function (err, connection) {
        // rows = 1
        console.log(rows);
        connection.query('SELECT * FROM messages', function (error, results, fields) {
            connection.release();
            if (error) throw error;
            // rows = 1
            rows = results;
            // rows = the expect output (array of objects)
        });
    });
    // rows = 1
    return rows;
}

Thanks for your help!



via aslad

No comments:

Post a Comment