Installed MongoDb but getting error while trying to connect through index.js file please refer attached codes

{“t”:{“$date”:“2022-06-23T09:36:26.430-04:00”},“s”:“I”, “c”:“NETWORK”, “id”:23016, “ctx”:“listener”,“msg”:“Waiting for connections”,“attr”:{“port”:27017,“ssl”:“off”}}

=== index.js ====

const mongoose = require('mongoose');

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

    .then(() => console.log('Connected to MongoDB...'))

    .catch((err) => console.log('Could not connect to MongoDB...', err))

=== error getting ===

Could not connect to MongoDB... MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\connection.js:847:32)
    at D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\index.js:351:10
    at D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\index.js:1149:10)
    at Mongoose.connect (D:\KBG\Ascent Class\Node.js\test\mongo-demo\node_modules\mongoose\lib\index.js:350:20)
    at Object.<anonymous> (D:\KBG\Ascent Class\Node.js\test\mongo-demo\index.js:3:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

I do see an error in your connection string. You are missing the port
Try adding the port information and see if you can connect.

mongodb://localhost:27017/playground
1 Like