Error handling in mongodb-connect

Now, before we write any data, I'm going to go ahead and handle any potential errors that come about. I'll do that using an if statement. If there is an error, we're going to print a message to the console, letting whoever is looking at the logs know that we were unable to connect to the database server, console.log, then inside of quotes put something like Unable to connect to MongoDB server. After the if statement, we can go ahead and log out a success message, which will be something like console.log. Then, inside of quotes, we'll use Connected to MongoDB server:

MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, client) => { if(err){ console.log('Unable to connect to MongoDB server'); } console.log('Connected ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.