node.jsmongoose

I'm getting this error during connecting mongoose to nodejs


I am New in Nodejs. I have just started learning about MongoDB and I am trying to host my nodejs application locally via MongoDB Server 6.0 (without using mongoose or atlas).

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/e-commerce')

And I am getting this error.

D:\Ecommerce-Dashboard\server\node_modules\mongoose\lib\connection.js:755
err = new ServerSelectionError();
      ^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at _handleConnectionErrors (D:\Ecommerce-Dashboard\server\node_modules\mongoose\lib\connection.js:755:11)   
    at NativeConnection.openUri (D:\Ecommerce-Dashboard\server\node_modules\mongoose\lib\connection.js:730:11) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 606629,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\Ecommerce-Dashboard\server\node_modules\mongodb\lib\cmap\connect.js:383:20)
            at Socket.<anonymous> (D:\Ecommerce-Dashboard\server\node_modules\mongodb\lib\cmap\connect.js:307:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
  },
  code: undefined
}

help to solve this error i am getting this error during connecting mongodb with node js.


Solution

  • Go to file manager and 1st stop and then start MongoDB

    enter image description here

    const mongoose = require('mongoose');
    mongoose.connect('mongodb://127.0.0.1/e-commerce')
    

    try it. Your nodejs app successfully connect to MongoDB