The 'GetUsers' function is running and it returns the correct values as expected. However, res.send(users) is running before the nested function has completed with the value so it is undefined. I have tried doing the res.send(users) from inside the nested function but it doesn't recognize the function. I have also tried using return users but can't get it to work. Any help would be appreciated!
app.get('/GetUsers', function (req, res){
    var url = 'mongodb://localhost:27017/users';
    var users = "";
    MongoClient.connect(url, function (err, db) {
        if (err) {
            console.log('Unable to connect to the mongoDB server. Error:', err);
        } else {
            console.log('Connection established to', url);
            // Get the documents collection
            var collection = db.collection('users');
            // Get some users
            collection.find().toArray(function (err, res) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Got Information from db.');
                }
                //Close connection
                db.close();
                console.log('db closed.');
                users = res;
                console.log(users);
            });
        }
    });
res.send(users);
});
 
     
     
    