avatarBryant Jimin Son

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1943

Abstract

ing.</li><li>MongoDB is up and running</li><li>I am using the correct MongoDB port number</li></ol><p id="6ff2">I also added few console.log(…) to make sure that the error happens during the line where <b>MongoClient.conect(…) </b>happens.</p><figure id="33e9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*F8i0lnIgvWSOI7ieA4qPmQ.png"><figcaption>Putting the console.log(…) debugging snippets</figcaption></figure><p id="978e">This indeed showed that the error happens at the line where MongoClient.connect(…) gets called, as I saw “Before calling MongoClient…” prints out but “After calling MongoClient…” does not.</p><figure id="06b0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*POm29c-J3yY8fvd900aHGA.png"><figcaption>Before Calling MongoClient… prints out while After calling MongoClient… does not</figcaption></figure><p id="6a79">When I check back Mongo service again, I noticed that the Mongo DB indeed exposes the service through port 27017.</p><figure id="87d6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j8IBpaA-I53XC0t_78202g.png"><figcaption>MongoDB is correctly exposed through the port 27017</figcaption></figure><p id="fdd4">Then, I had this thought.</p><blockquote id="261f"><p>“But wait a second. What if I change localhost to 127.0.0.1?”</p></blockquote><p id="dfc6">Well, localhost should be mapped to 127.0.0.1, but what if MongoDB cannot figure that out for some reason? I decided to change localhost to 127.0.0.1, save, and try to run NodeJS again.</p><figure id="785a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*PvtLUM1gauQByMdEtSaw0Q.png"><figcaption>I changed localhost to 127.0.0.1 and tried to run again</figcaption></figure><p id="1816">I called the same API endpoint, and it correctly returned the response this time. So, from the stand point of the server, this mapping is usually configured through <b>/etc/hosts.</b> I susp

Options

ected whether this got changed anytime in the past. I opened /etc/hosts but found localhost still correctly maps to 127.0.0.1.</p><figure id="c941"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*odgO-Xp17gIlccHe8E7H8Q.png"><figcaption></figcaption></figure><p id="7b07">I am now suspecting this has to Mongo DB configuration. I know, in my Mac, that the MongoDB configurations are stored in the file <b>/usr/local/etc/mongod.conf, </b>so I opened that file using VIM.</p><p id="6ce6">I noticed that bindIp value is set to 127.0.0.1.</p><figure id="3250"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GaZMQxpjs4YkcK2pn5oXUg.png"><figcaption></figcaption></figure><p id="f44e">So, in the version prior to MongoDB 3.6, Mongo DB was binding to 127.0.0.1. From MongoDB 3.6 and beyond, it will find to localhost. See:</p><div id="9159" class="link-block"> <a href="https://docs.mongodb.com/manual/core/security-mongodb-configuration/"> <div> <div> <h2>IP Binding - MongoDB Manual</h2> <div><h3>Starting in MongoDB 3.6, MongoDB binaries, and , bind to localhost by default. If the configuration file setting or the…</h3></div> <div><p>docs.mongodb.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*sScQcAQJySNqibWp)"></div> </div> </div> </a> </div><p id="55eb">In any case, setting bindIP a value explicitly sets the Mongo DB to listen to that port only. And this is why localhost did not work because I was using Mongo DB 3.2.3.</p><figure id="43da"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0VU478h2wftYh0zme9N8Pg.png"><figcaption>Since I was using an earlier version of Mongo, the bindIp was pointing to 127.0.0.1.</figcaption></figure></article></body>

How to resolve “Error Connecting to db. MongoNetworkError” when connecting to localhost MongoDB through MongoClient from NodeJS

How to resolve error “Error Connecting to db” “MongoNetwork Error” when using MongoClient

So, to connect Mongo DB in NodeJS, I had to use MongoClient, which was imported using a command

import { MongoClient } from ‘mongodb’;

Way to import MongoClient from NodeJS

I used MongoClient code snippet below to connect to a local Mongo DB from NodeJS using the following command.

const client = await MongoClient.connect(“mongodb://localhost:27017”, { useNewUrlParser: true });

Using MongoClient to connect to a local MongoDB running in port 27017

However, when I was hitting the API endpoint through PostMan, which is a HTTP client tool more user friendly and advanced than curl, I was getting the following error.

{ “message”: “Error connecting to db”, “error”: { “name”: “MongoNetworkError” } }

MongoNetworkError shown from PostMan when hitting the API endpoint

This is odd. I verified few things:

  1. Make sure that I am calling the correct API endpoint — This is not going to be a problem since it complains that MongoDB is not responding.
  2. MongoDB is up and running
  3. I am using the correct MongoDB port number

I also added few console.log(…) to make sure that the error happens during the line where MongoClient.conect(…) happens.

Putting the console.log(…) debugging snippets

This indeed showed that the error happens at the line where MongoClient.connect(…) gets called, as I saw “Before calling MongoClient…” prints out but “After calling MongoClient…” does not.

Before Calling MongoClient… prints out while After calling MongoClient… does not

When I check back Mongo service again, I noticed that the Mongo DB indeed exposes the service through port 27017.

MongoDB is correctly exposed through the port 27017

Then, I had this thought.

“But wait a second. What if I change localhost to 127.0.0.1?”

Well, localhost should be mapped to 127.0.0.1, but what if MongoDB cannot figure that out for some reason? I decided to change localhost to 127.0.0.1, save, and try to run NodeJS again.

I changed localhost to 127.0.0.1 and tried to run again

I called the same API endpoint, and it correctly returned the response this time. So, from the stand point of the server, this mapping is usually configured through /etc/hosts. I suspected whether this got changed anytime in the past. I opened /etc/hosts but found localhost still correctly maps to 127.0.0.1.

I am now suspecting this has to Mongo DB configuration. I know, in my Mac, that the MongoDB configurations are stored in the file /usr/local/etc/mongod.conf, so I opened that file using VIM.

I noticed that bindIp value is set to 127.0.0.1.

So, in the version prior to MongoDB 3.6, Mongo DB was binding to 127.0.0.1. From MongoDB 3.6 and beyond, it will find to localhost. See:

In any case, setting bindIP a value explicitly sets the Mongo DB to listen to that port only. And this is why localhost did not work because I was using Mongo DB 3.2.3.

Since I was using an earlier version of Mongo, the bindIp was pointing to 127.0.0.1.
Mongo
Mongodb
Database
NoSQL
Nodejs
Recommended from ReadMedium