简体   繁体   English

捕获错误的正确方法是什么?

[英]What is the correct way of catching an error?

How can i set a variable to the message of an error? 如何为错误消息设置变量? This is my code: 这是我的代码:

 .catch(console.error).then((err) => {
  var x = err.message;
 )};

And i am getting this error: 我收到此错误:

Cannot read property 'message' of undefined 无法读取未定义的属性“消息”

You get the error in the catch call: 您在catch调用中得到错误:

.catch((err) => {
    console.error(err);
    var x = err.message;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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