0

I tried to connect mongodb in node js.but its throwing this type error.

Error: getaddrinfo ENOTFOUND mongodb mongodb:27017

How to fix it.any one can give me solution

index.js

const MongoClie

nt = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID
const url = "mongodb://olcadmin:olcmongodb@mongodb:27017/admin";
const client = new MongoClient.connect(url, { useNewUrlParser: true },function(err,db)
{
    if(err)
    {
        console.log(err)
    }
    else
    {
        console.log("Connected Sucessfully")
    }
});


Connected Successfully  

but I got This type error:

{ Error: getaddrinfo ENOTFOUND mongodb mongodb:27017
    at errnoException (dns.js:50:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'mongodb',
  host: 'mongodb',
  port: 27017 }
3
  • 1
    Is there a locally resolve host named mongodb? Commented May 8, 2019 at 5:53
  • The URL address is somewhat wrong Commented May 8, 2019 at 5:56
  • To add more detalis to what Sirko mentioned, you should have a line that looks like 127.0.0.1 mongodb in your /etc/hosts file. Commented May 8, 2019 at 9:42

1 Answer 1

1

Try "mongodb://olcadmin:[email protected]:27017/admin" instead of "mongodb://olcadmin:olcmongodb@mongodb:27017/admin", if you are using local mongodb

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.