cb 不是函数 - 使用 winston 处理未处理的 Promise Rejection 时

cb is not a function - when handling the unhandled Promise Rejection using winston

当我 运行 此代码使用 winston 处理未处理的 Promise 拒绝时,获取 cb 不是一个函数。在控制台中,它显示了确切的错误,但它存储了 cb is not a function。

winston.add(winston.transports.File, {
  filename: "logfile.log"
});
winston.add(winston.transports.MongoDB, {
  db: "mongodb://localhost/vidly-practice",
  level: "info"
});
process.on("uncaughtException", ex => {
  console.log("WE GOT UNHANDLED EXCEPTION");
  winston.error(ex.message, ex);
});
process.on("unhandledRejection", ex => {
  winston.error(ex.message, ex);
  process.exit(1);
});
const p = Promise.reject(new Error("Unhandled rejection.........!!!"));
p.then(() => console.log("Done"));

Listening on port 3000... error: Unhandled rejection.........!!! Error: Unhandled rejection.........!!! at Object. (F:\Node js Course[FreeCoursesOnline.Me] CodeWithMosh - The Complete Node.js Course- Mongoose- Modeling Relationships between Connected Data.7- Project- Build the Rentals API\after\vidly\index.js:36:26) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) at internal/main/run_main_module.js:17:11

免责声明:我不使用 winston。

您将 ex 作为第二个参数传递,它需要一个函数。在您给定的代码中, ex 不是函数,而是 Error 或类似内容的实例。

您应该像下面的代码片段那样做。

// NOTE: this code is not tested
process.on("unhandledRejection", (ex) => {
     winston.error(ex.message, () => { // this should be your callback
         // do something inside your callback function
     });
     process.exit(1);
});
// NOTE: this code is not tested

或者,如果您了解 TypeScript,请阅读打字:https://github.com/winstonjs/winston/blob/master/index.d.ts#L60

或者这一个,适合你的情况:https://github.com/winstonjs/winston/blob/master/index.d.ts#L68