【问题标题】:User defined error does not get printed未打印用户定义的错误
【发布时间】:2018-04-15 13:36:51
【问题描述】:
axios.get(geocodeUrl).then((response) =>
{
  if(response.data.status === 'ZERO_RESULTS')
  {
    throw new ERROR('Unable To Find That Address');
  }
  console.log(JSON.stringify(response.data, undefined ,2));
}).catch((e)=> {
  if(e.code === 'ENOTFOUND')
  {
    console.log('Unable to contact api servers');
  }
  else {
    console.log(e.message);
    console.log("xyz");
  }
});

打印出来:

"Error is not defined"

而不是

"Unable to find that address"xyz 被打印出来。

【问题讨论】:

  • ERROR 更改为Error

标签: node.js error-handling


【解决方案1】:

这里你犯了一个语法错误,即ERROR不应该用大写字母写它应该像Error。这就是它无法识别 Error 关键字的原因。

所以更正就在这一行:

throw new Error('Unable To Find That Address');

【讨论】:

    【解决方案2】:

    您一定没有让 jQuery 可用于您的脚本。

    将此添加到文件顶部:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    

    【讨论】:

    • 说真的,这与 OP 问题有什么关系。它在 node.js 中有明确的标记,即使它与 jQuery 无关:Error
    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多