user12954623
user12954623

Reputation:

Can't connect to Mongodb database

I set up my app to connect to the mongodb database using mongoose, and thought I did it correctly; however, when I ran the app, I got the error message below.

//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
  }
}

Can you point me in the right direction on how to fix this issue. The app ran for about 1 minute before crashing so I know that the server was set up correctly. Thanks in advance.

Upvotes: 0

Views: 731

Answers (1)

lissettdm
lissettdm

Reputation: 13078

Remove the http part. According to the official docs it is not necessary:

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

*If connecting fails on your machine, try using 127.0.0.1 instead of localhost (taken from docs)

Upvotes: 1

Related Questions