无法连接到 Mongodb 数据库

时间:2021-05-14 14:32:08

标签: mongodb express mongoose

我将我的应用程序设置为使用 mongoose 连接到 mongodb 数据库,并认为我做得对;但是,当我运行该应用程序时,我收到以下错误消息。

//database
mongoose.connect('mongodb://http://localhost:27017/userDB', {useNewUrlParser: true, useUnifiedTopology: true });


//ERROR

const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: getaddrinfo ENOTFOUND http
    at NativeConnection.Connection.openUri (/Users/blkboxng/Desktop/publicTradesProperties/mentorsparlor/node_modules/mongoose/lib/connection.js:846:32)


reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) {
      'http:27017' => ServerDescription {
        address: 'http:27017',
        error: Error: getaddrinfo ENOTFOUND http
            at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:69:26) {
          name: 'MongoNetworkError'
        },
        roundTripTime: -1,
        lastUpdateTime: 859005782,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

你能指出我如何解决这个问题的正确方向吗?该应用程序在崩溃前运行了大约 1 分钟,所以我知道服务器设置正确。提前致谢。

1 个答案:

答案 0 :(得分:0)

删除 http 部分。根据官方 docs 的说法是没有必要的:

mongoose.connect('mongodb://localhost:27017/userDB', {useNewUrlParser: true, useUnifiedTopology: true });

*如果在您的机器上连接失败,请尝试使用 127.0.0.1 而不是 localhost(取自文档)