0

I am trying to create a real time chatting app but when i try connecting my mongodb using a url in dotenv i get mongoose server selection error.

The error is as shown below.

server started on port ${process.env.PORT}
C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\connection.js:807
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\connection.js:807:32)
    at C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\index.js:343:10
    at C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\index.js:1181:10)
    at Mongoose.connect (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongoose\lib\index.js:342:20)
    at Object.<anonymous> (C:\Users\Admin\Desktop\CHAT APP\server\index.js:12:10)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 1858768265,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
            at connectionFailureError (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongodb\lib\cmap\connect.js:375:20)
            at Socket.<anonymous> (C:\Users\Admin\Desktop\CHAT APP\server\node_modules\mongodb\lib\cmap\connect.js:295:22)
            at Object.onceWrapper (node:events:646:26)
            at Socket.emit (node:events:526:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          [Symbol(errorLabels)]: Set(0) {}
        }
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}
[nodemon] app crashed - waiting for file changes before starting...

The following is the main code and the .env file that has the conn URL//

 const express = require("express")
    const cors = require("cors")
    const mongoose = require("mongoose")
    
    const app = express()
    require("dotenv").config()
    
    app.use(cors())
    app.use(express.json())
    
    
    mongoose.connect(process.env.MONGO_URL,{
        useNewUrlparser: true,
        useUnifiedTopology:true, 
    })
    
    const server= app.listen(process.env.POR, () => {
        console.log('server started on port ${process.env.PORT}')
    })

The .envfile has the following

PORT=5000
MONGO_URL= "mongodb://localhost:27017/chat" 
James Z
  • 12,104
  • 10
  • 27
  • 43

0 Answers0