We help IT Professionals succeed at work.

Am I explaining the mechanics of this database connection correctly?

High Priority
180 Views
Last Modified: 2019-05-06
const mongodb = require("mongodb");
const MongoClient = mongodb.MongoClient;

const mongoConnect = callback => {
  MongoClient.connect(
    "mongodb+srv://brucegust:Mu5cular!@brucegust-qhxnz.mongodb.net/test?retryWrites=true"
  )
    .then(client => {
      console.log("Connected");
      callback(client);
    })
    .catch(err => {
      console.log(err);
    });
};

mongoConnect(client => {
  console.log(client);
  app.listen(3000);
});

Open in new window


Am I explaining what I have above correctly...?

"mongoClient" is a constant that "houses" the functionality documented on lines 4-15. We trigger that functionality by referencing the mongoConnect constant on line 17. We pass an anonymous function as an argument to the mongoClient function which is going to be represented by the word "callback" which you can see on line four.

The mongoClient function is actually a "promise" which gives us the opportunity to structure an "IF / ELSE" scenario based on the successful completion of a callback. In this case, the callback that we're most interested in is on line 5-6 where we're grabbing the "mongoClient" and connecting to the database. If that callback happens successfuly, we proceed to the "then" block. As per the way "promises" are structured, we have access to the result of the successful callback as a variable which, in this case, we are calling "client." The "client" variable, which is a successful connection to the database, is passed to the callback which is then going to document the details of that connection (line #18) and we're also going to see "connected" in the console thanks to line #9.

Is that right?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
"mongoClient" is a constant that "houses" the functionality documented on lines 4-15
In theory, mongoClient is a constant that refers  to the MongoDB client module. In your MongoDB client module there will be some exported functionality - by assigning that module to mongoClient you gain access to that exported functionality (methods basically in this case)

We trigger that functionality by referencing the mongoConnect constant on line 17. We pass an anonymous function as an argument to the mongoClient function which is going to be represented by the word "callback" which you can see on line four.
Yup - in the connect method we are going to issue a callback to a supplied function when the connection is complete (line 10).

The mongoClient function is actually a "promise" which gives us the opportunity to structure an "IF / ELSE" scenario based on the successful completion of a callback.
The mongoClient.connect() function returns a promise.

The "client" variable, which is a successful connection to the database, is passed to the callback which is then going to document the details of that connection (line #18)
At this point a console.log if that is what is meant by document
 ss177.jpg
Bruce GustPHP Developer

Author

Commented:
This is great, Julian! Thanks!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.