Javascript 如何处理无法处理的错误?

Javascript 如何处理无法处理的错误?,javascript,mongoose,Javascript,Mongoose,我在NodeJS中遇到了错误处理问题, 在测试我制作的应用程序时,我注意到如果我错误地将undefined传递给mongoose.connect(),它将给您一个错误,不幸的是,回调函数没有捕捉到这个错误,try&catch块也没有捕捉到这个错误: const envVariable = undefined; try { mongoose.connect(envVariable, (err) => { if (err) return console.log("The

我在NodeJS中遇到了错误处理问题, 在测试我制作的应用程序时,我注意到如果我错误地将undefined传递给
mongoose.connect()
,它将给您一个错误,不幸的是,回调函数没有捕捉到这个错误,try&catch块也没有捕捉到这个错误:

const envVariable = undefined;

try {
  mongoose.connect(envVariable, (err) => {
    if (err) return console.log("There was an error");
    console.log("success");
  });
} catch (error) {
  console.log("Hah! I caught you");
}
如果您看到未捕获错误,请参阅输出:

(node:36807) UnhandledPromiseRejectionWarning: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
    at NativeConnection.Connection.openUri (.../nodeJS/tests/node_modules/mongoose/lib/connection.js:680:11)
    at .../nodeJS/tests/node_modules/mongoose/lib/index.js:345:10
    at .../nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (.../nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (.../nodeJS/tests/node_modules/mongoose/lib/index.js:1135:10)
    at Mongoose.connect (.../nodeJS/tests/node_modules/mongoose/lib/index.js:344:20)
    at Object.<anonymous> (.../nodeJS/tests/server.js:8:12)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:36807) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:36807) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(节点:36807)未经处理的PromisejectionWarning:MongooseError:`openUri()`的`uri`参数必须是字符串,且为“未定义”。确保`mongoose.connect()`或`mongoose.createConnection()`的第一个参数是字符串。
在NativeConnection.Connection.openUri(…/nodeJS/tests/node_modules/mongoose/lib/Connection.js:680:11)
在…/nodeJS/tests/node_modules/mongoose/lib/index.js:345:10
在…/nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
在新的承诺()
在promiseOrCallback(…/nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
在Mongoose._promiseOrCallback(…/nodeJS/tests/node_modules/Mongoose/lib/index.js:1135:10)
在Mongoose.connect(…/nodeJS/tests/node_modules/Mongoose/lib/index.js:344:20)
反对。(…/nodeJS/tests/server.js:8:12)
at模块编译(内部/modules/cjs/loader.js:1063:30)
在Object.Module._extensions..js(internal/modules/cjs/loader.js:1092:10)
在Module.load(内部/modules/cjs/loader.js:928:32)
at Function.Module._load(内部/modules/cjs/loader.js:769:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:72:12)
在internal/main/run_main_module.js:17:47
(使用`node--trace warnings…`显示警告的创建位置)
(节点:36807)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:36807)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
在我的应用程序中,我已经多次遇到此类问题,但这次我在这里抓住了一个问题来展开讨论,我如何才能阻止此类错误出现在我的脸上?相反,用一种好的方式处理它们


handle=捕获此问题发生的时间,并向客户端发送一个来自服务器端的问题的良好响应。

连接字符串和选项对象之后的
moongoose.connect
函数。它返回一个承诺,该承诺会因错误而被拒绝,但您永远不会处理它

正确的语法是

try {
  await mongoose.connect(envVariable);
  console.log("success");
} catch (error) {
  console.log("Hah! I caught you");
}


顺便说一句,我一开始就在代码中添加了这一行,但它也没有捕捉到错误
process.on(“uncaughtException”,(err)=>{console.log(
uncaughterror:
,err.name,err.message);})请在问题帖子中添加新信息。它不属于注释。谢谢,@Bergi,请更正第一个代码好吗?它正在等待,但不是在异步function@enwuser然后将它所在的函数设置为异步,或者使用第二种方法。
mongoose.connect(envVariable).then(() => {
  console.log("success");
}, (err) => {
  console.log("Hah! I caught you");
});