我试图在节点 js 中连接 mongodb。但它抛出了这种类型的错误。
Error: getaddrinfo ENOTFOUND mongodb mongodb:27017
如何解决它。任何人都可以给我解决方案
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
但我得到了这种类型的错误:
{ 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 }