1

I'm making a web application using node.js, express and mysql. I need to change this variable num with the data that I get from the SQL query. Do you know how I can change this variable inside a callback? Or how else can I get this data from this query. I have seen many questions about a similar problem, but none of them helped me :(

router.get('/:filters?', function(req, res, next) {
    let num;
    if (!req.params.filters) {
        connect.query('SELECT COUNT(*) FROM STUDENTS', (err, res) => {
            num = res[0];
        });

        console.log(num); // => undefined
        //...
    }
});
Seba
  • 95
  • 1
  • 12
  • Do these answer your question? https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call, https://stackoverflow.com/questions/23339907/returning-a-value-from-callback-function-in-node-js – cbr Mar 13 '20 at 18:32
  • "Do you know how I can change this variable inside a callback?" — The code you have will do that … just not before the log function is called. – Quentin Mar 13 '20 at 18:33

0 Answers0