【问题标题】:cb is not a function - when handling the unhandled Promise Rejection using winstoncb 不是函数 - 使用 winston 处理未处理的 Promise Rejection 时
【发布时间】:2020-03-01 13:32:17
【问题描述】:

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

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"));

侦听端口 3000... 错误:未处理的拒绝.........!!! 错误:未处理的拒绝…………!!! 在对象。 (F:\Node js Course[FreeCoursesOnline.Me] CodeWithMosh - 完整的 Node.js 课程\9- Mongoose- 建模 连接数据之间的关系\9.7-项目-建造租赁 API\after\vidly\index.js:36:26) 在 Module._compile (internal/modules/cjs/loader.js:956:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) 在 Module.load (internal/modules/cjs/loader.js:812:32) 在 Function.Module._load (internal/modules/cjs/loader.js:724:14) 在 Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) 在内部/main/run_main_module.js:17:11

【问题讨论】:

  • 缺少代码并且关于您的计划的信息太少。请添加更多信息。
  • 请检查代码中缺少什么?我在 logfile.log 中存储错误消息和错误,它显示的输出带有确切的错误,但在日志文件中它显示 cb 不是函数

标签: node.js mongodb express winston


【解决方案1】:

免责声明:我不使用winston。

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

你应该像下面的 sn-p 那样做。

// 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2019-11-10
    • 2020-09-13
    • 1970-01-01
    相关资源
    最近更新 更多