I am making a little game in node.js and as database I am using mongodb with mongojs. Currently this is the way I am handling the database conenction:
var mongojs = USE_DB ? require("mongojs") : null;
var db = USE_DB ? mongojs('localhost:27017/dbName', ['players','pets']) : null;
And for example If I want to check if username is taken upon registration, I do:
Database.usernameCheck= function(name,callback){
db.account.findOne({username:name.username},function(err,result){
if(result) callback(true);
else callback(false);
});
}
And then I call the function when a new player is registered:
Database.usernameCheck(data,function(result){
if(result){
//username taken
} else {
//add the user
}
});
I am not too much experienced with the node.js and javascript programming, but I read that the best way to do is with promises, so I would like to convert my current code to use promises instead of callbacks. Tried a couple of ways, but I failed so I decided to ask here, well, thank you for your time.