简体   繁体   中英

Local Mongoose Connection Timing Out And Crashing

I am working on setting up a very simple full stack web application that can handle users signing up and logging in. For this, I have employed mongoose as a local database to store my users information after signup. This was working for a long time, but I recently ran my app after a week away from it and this happened:

    MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/connection.js:845:32)
    at /Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:345:10
    at /Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:1135:10)
    at Mongoose.connect (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:344:20)
    at Object.<anonymous> (/Users/hca/Desktop/fullstacksus/server.js:26:10)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  reason: TopologyDescription {
    type: 'Unknown',
    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
  }
}
node:internal/process/promises:225
          triggerUncaughtException(err, true /* fromPromise */);
          ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/connection.js:845:32)
    at /Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:345:10
    at /Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:1135:10)
    at Mongoose.connect (/Users/hca/Desktop/fullstacksus/node_modules/mongoose/lib/index.js:344:20)
    at Object.<anonymous> (/Users/hca/Desktop/fullstacksus/server.js:26:10)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  reason: TopologyDescription {
    type: 'Unknown',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        error: Error: connect ECONNREFUSED 127.0.0.1:27017
            at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
          name: 'MongoNetworkError'
        },
        roundTripTime: -1,
        lastUpdateTime: 57762916,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}
[nodemon] app crashed - waiting for file changes before starting...

For reference, this is a very stripped down version of my server.js file:

if (process.env.NODE_ENV !== "production") {
    require('dotenv').config({ path: '.env' });
}

const express = require('express');
const mongoose = require('mongoose');
const app = express();

mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true, 'useCreateIndex': true });
mongoose.connection.on('error', error => console.log(error));
mongoose.connection.once('open', () => console.log("Connected To Database"));

const indexRouter = require('./routes/index');

app.use('/', indexRouter);

app.listen(3030);

and this is my .env file :

MONGO_URL=mongodb://localhost/fullstacksus

very simple, and, as of a week ago working perfectly.

I think you have to run the MongoDB server as well before starting the project. you may use any of the following, (Supposedly, you already have installed the mongodb in your system).

I will suggest you to install the mongodb community edition from the following link https://docs.mongodb.com/manual/administration/install-community/

Then after installing it. Just start the service in a terminal by the command

  • Ubuntu

    • sudo systemctl start mongod
  • Windows After setting the environment variable path to mongod.exe run the following command in the terminal.

    • mongod

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM