使用mongoose连接到MongoDb - 错误:getaddrinfo ENOTFOUND

时间:2022-10-04 12:54:31

I have following config:

我有以下配置:

  "mongoose": {
    "url": "mongodb://127.0.0.1:27017/chat",
    "options": {
      "server": {
        "socketOptions": {
          "keepAlive": 1
        }
      }
    }
  }

And connect to my DB

并连接到我的数据库

mongoose.connect(config.get('mogoose:url'), config.get('mongoose:options'))

But I'm getting such error:

但是我收到了这样的错误:

node_modules/mongoose/node_modules/mongodb/lib/server.js:236
        process.nextTick(function() { throw err; })
                                      ^
Error: getaddrinfo ENOTFOUND undefined undefined:27017
    at errnoException (dns.js:27:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:26)

I have already checked answers for simular question.

我已经检查了类似问题的答案。

I'm quite new in Mongo, but following code works fine using native driver:

我在Mongo中很新,但是使用本机驱动程序后代码工作正常:

var MongoClient = require('mongodb').MongoClient
  , format = require('util').format;

MongoClient.connect('mongodb://127.0.0.1:27017/chat', function(err, db) {
  if (err) throw err;
//blabla
}

So answers for that question are not actual in my case.

因此,对于这个问题的答案在我的案例中并不实际。

1 个解决方案

#1


8  

Problem in your code is that you typo here config.get('mogoose:url'). You missed a n in mongoose. Thats why you trying connect to undefined:27017

你的代码中的问题是你在这里拼错了config.get('mogoose:url')。你错过了猫鼬的n。这就是为什么你尝试连接到undefined:27017

#1


8  

Problem in your code is that you typo here config.get('mogoose:url'). You missed a n in mongoose. Thats why you trying connect to undefined:27017

你的代码中的问题是你在这里拼错了config.get('mogoose:url')。你错过了猫鼬的n。这就是为什么你尝试连接到undefined:27017