0

Why is this not returning correctly? Can anyone help? It gets the correct value from the database (rows[0] is correct data), but I just cant figure out how to return it properly.

module.exports = {
    get: function(query) {
        var value = pool.getConnection(function(err, con) {
            if (err) throw err;

            con.query(query, function(err, rows) {
                if (err) throw err;

                con.release();
                console.log(rows[0]); //Correctly prints
                return rows;
            })
        })
        console.log(value); //Prints undefined
        return value[0]
    }
}
Sean
  • 41
  • 1
  • 4
  • 1
    Side note: That's not a "JavaScript module" (we have those now, defined by the ES2015 spec), it's a Node module. – T.J. Crowder Dec 30 '17 at 16:44
  • Seems this is the typical problem of returning response from asynchronous call – brk Dec 30 '17 at 16:50
  • How could I solve this problem then? I need to wait for a callback to finish only if paths[1] == "user" – Sean Dec 30 '17 at 17:20

0 Answers0