I am working on a node.js project in which I need to communicate with a mongoDb database. I am currently programming a function to find some data in my db using th node-mongodb-native module. Everything works, but my code looks like a callback in a callback in a callback in a callback...
I created this function to prevent me from using callbacks every time I want to access my database. I now just need to call this function.
module.exports.find = function(query, projection, callback){
db.open(function(err, db){
if(err) throw err;
db.collection('rooms', function(err, collection){
if(err) throw err;
collection.find(query, projection, function(err, cursor){
if (err) throw err;
cursor.toArray(function(err, find){
db.close();
callback(err, find);
});
});
});
});
};
Is there a method to reduce this codeception ?