Error with Mongoose 5.13.2 and MongoDB Node.js driver 3.6.8 when attempting to connect to local database

This version of Mongoose uses mongodb@3.6.8.

If I open MongoDB Compass v1.28.1 it connects successfully to the database and I can add documents and collections freely. This is on a Windows 10 computer.

On another computer with Linux the same code connects to the local database successfully.

The connection string used is: mongodb://localhost:27017/some-database-name-here.

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [connect ECONNREFUSED
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
  name: 'MongoNetworkError'
    at Pool.<anonymous> (/path/to/my/project/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:314:20)
    at /path/to/my/project/node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /path/to/my/project/node_modules/mongodb/lib/core/connection/pool.js:995:11
    at /path/to/my/project/node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/path/to/my/project/node_modules/mongodb/lib/core/connection/connect.js:280:5)
    at Socket.<anonymous> (/path/to/my/project/node_modules/mongodb/lib/core/connection/connect.js:310:7)
    at Object.onceWrapper (events.js:421:26)
    at Socket.emit (events.js:314:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

The code, that I think is relevant, is this:

mongoose.connect(connStr, {
  useNewUrlParser: true,
  sslCA: caExists && caContents ? caContents : undefined

Please advice.

Thank you.

If I use the local IP address I get:

MongoNetworkError: failed to connect to server [] on first connect [MongoNetworkTimeoutError: connection timed out

Indicates the no process is listening at this address. If it worked with Compass before, then mongod might have been terminated. Try restarting mongod.

1 Like