Q: Returning id from node.js is not working EDIT: The problem is that the console.log(theID) executes before the value is assigned in the query callback. So my problem now is how to have the query callback to run first so that theID is assigned to a value before I return it
Here is the Server.js.
app.get('/testtwilio2', function(req,res,next){
    var theID;
    var p;
    var config;
     var pat = {name: req.query.name, number: req.query.number, date:req.query.date, reminded: 0, paid:0 };
        connection.query('INSERT INTO patientlist SET ?', pat, function(err,res){

        if(err) throw err;
             theID = res.insertId

            console.log('Last insert ID:', theID); //returns correct value
             p = {id: theID, name: req.query.name, date: req.query.date, number: req.query.number, reminded: req.query.reminded};
        var fs = require('fs');
        var configFile = fs.readFileSync('./Public/worked.json');
        var config = JSON.parse(configFile);

        var object = req.query.name;
        config.push(p);
        var configJSON = JSON.stringify(config);
        fs.writeFileSync('./Public/worked.json', configJSON);


        });
        console.log(theID); //this executes first meaning theID hasn't been assigned a value
        return res.json(theID);
});

