Returning Data Asynchronously from Model Layer in Node.js App

I'm working on a node application and have structured it with a separate model layer to organize my code. The model has a function called getImages that makes use of the Mongojs module to fetch information from the database. The problem is that getImages returns before the data is retrieved. I'm used to working with synchronous code and so this problem is proving difficult to wrap my head around. I suspect the answer is somehow to employ a callback function. Can someone show how that would work? Or if a callback is not the solution, what is?

routes/index.js

exports.image = function(req, res) {
if (req.method == 'GET') {

    var image = model.getImages();
    return res.send(image);        
}
}

models/index.js

var getImages = function() {

    var imagesCollection = db.collection('Images');

    var images = imagesCollection.find(function(err, docs) {
        return docs;
    });

    return images;
}

exports.getImages = getImages;

You need to provide a callback parameter to your getImages function. As in:

routes/index.js

exports.image = function(req, res) {
    if (req.method == 'GET') {
        model.getImages(function(err, images) {
            return res.send(images);        
        });
    }
}

models/index.js

var getImages = function(callback) {

    var imagesCollection = db.collection('Images');

    imagesCollection.find(callback);
}

exports.getImages = getImages;

Seems like you are imagesCollection.find is Asynchronous function.Here is the code what oyu can try

var getImages = function(callback) {

  var imagesCollection = db.collection('Images');

  imagesCollection.find(function(err, docs) {
     if (err) {
       callback(null);
     } else {
        callback(docs);
     }

  });

}


exports.getImages = getImages;
exports.image = function(req, res) {

 var imageDataFunc = function(imageDataFromDB) {
  if(imageDataFromDB) {
     res.send(imageDataFromDB);
     res.end();
  }

 }

 if (req.method == 'GET') {
   model.getImages(imageDataFunc);        
 }
}