Akash Kumar
Akash Kumar

Reputation: 1

Error resolve in connecting mongoose to node

const mongoose = require('mongoose');
main().catch(err => console.log(err));
 async function main() {

await mongoose.connect('mongodb://localhost:27017/test'); }

error

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32)    at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)    
at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:4:10)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149: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: 194037884, lastWriteDate: 0, error: MongoNetworkError: connect ECONNREFUSED ::1:27017 at connectionFailureError (E:\Web Dev\Backend\New folder\node_modules\mongodb\lib\cmap\connect.js:381:20)
at Socket. (E:\Web Dev\Backend\New folder\node_modules\mongodb\lib\cmap\connect.js:301:22)
at Object.onceWrapper (node:events:640:26) at Socket.emit (node:events:520:28) at emitErrorNT (node:internal/streams/destroy:164:8) at emitErrorCloseNT (node:internal/streams/destroy:129:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) } }, stale: false, compatible: true,

} }

Upvotes: 0

Views: 743

Answers (1)

ask4you
ask4you

Reputation: 808

Maybe this does the trick:

async function main () {
  const connectionOptions = {
    useNewUrlParser: true,
    useUnifiedTopology: true
   }
   
   try {
    await moongose.connect(/*Your URL*/,connectionOptions)
    console.log(`Connected to MongoDB`)
   } catch (err) {
    console.log(`Couldn't connect: ${err}`)
   }
}

Upvotes: 0

Related Questions