vendredi 15 avril 2016

MongoDb and expressJS trying to undertand Code

  function _allUsers(callback){
     var db = connect.get();

    db.collection("users").find({}).toArray(function(err,data){
      if(err){
        callback(err);
    }else{
        callback(null,data);
    }
 });
}

I am trying to understand this code, I have been looking around the web but I find the explanations kinda defficult to understand ( I am new at Mean stack), so my questions are:

  1. What does the Collection method do? I am not sure but the string "users" is it just the name of our collection with all users?

  2. Why do we have to use a callback in this situation? (I find callbacks very confusing).

  3. And why do we have to give toArray function, an annonymous function?

  4. Instead of toArray could I use pretty method() without any annonymous function as a parameter?




Aucun commentaire:

Enregistrer un commentaire