首页 > 解决方案 > Mongoose 异常的事件名称

问题描述

我正在使用 winston 记录我的错误,但我遇到了一个死点。我在互联网上搜索,但我什么也没找到。也许你们可以在这里帮助我。

我有一个错误,我喜欢使用例如 process.on("Eventname", (ex) => { blah blah blah}) 来捕捉它

下面是我的错误:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/connection.js:846:32)
From previous event:
    at NativeConnection.Connection.openUri (/Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/connection.js:849:10)
    at /Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/index.js:351:10
    at /Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
From previous event:
    at promiseOrCallback (/Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/index.js:1149:10)
    at Mongoose.connect (/Volumes/Seagate/Project/Noosy/node_modules/mongoose/lib/index.js:350:20)
    at connectDB (/Volumes/Seagate/Project/Noosy/DB/connect.js:4:19)
    at module.exports (/Volumes/Seagate/Project/Noosy/startup/db.js:12:9)
    at Object.<anonymous> (/Volumes/Seagate/Project/Noosy/index.js:12:24)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79: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 {
        address: 'localhost:27017',
        error: Error: connect ECONNREFUSED 127.0.0.1:27017
            at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1132:16) {
          name: 'MongoNetworkError'
        },
        roundTripTime: -1,
        lastUpdateTime: 19232686,
        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...

这是我的日志文件:

const config = require("config");
require("express-async-errors");
const winston = require("winston");
require("winston-mongodb");

module.exports = function () {
  winston.handleExceptions(
    new winston.transports.File({ filename: "logs/uncaughtExceptions.log" })
  );

  process.on(**"Mongoose Event Name"**, (ex) => {
    console.log("WE GOT AN UNCAUGHT EXCEPTION");
    winston.error(ex.message, ex);
    process.exit(1);
  });

  process.on("unhandledRejection", (ex) => {
    throw ex;
  });

  winston.add(new winston.transports.File(), { filename: "logs/logfile.log" });
  winston.add(new winston.transports.MongoDB(), {
    db: config.get("mongoDB.connString"),
    level: "info",
  });
};

任何帮助都非常受欢迎

非常感谢您并继续编码!

标签: javascriptnode.jsmongoose

解决方案


推荐阅读