node报错,找不到原因

代码调试了n遍,都正常,node为什么会报如下错误:

(node:18) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property '1' of null
(node:18) 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.

这是代码,调试的一切正常

...
case 49:
    let title = content.match(/<title>(.*)<\/title>/)[1];
    let des = content.match(/<des>(.*)<\/des>/)[1];
    let link = content.match(/<url>(.*)<\/url>/)[1];

    content = `APP:` +
        `标题:${title}\n` +  //这里的变量都显示正常
        `描述:${des}\n` +    //这里的变量都显示正常
        `链接:${link}`;      //这里的变量都显示正常
    break;
...
阅读 5.2k
2 个回答

看报错信息很明显

let title = content.match(/<title>(.*)<\/title>/)[1];
    let des = content.match(/<des>(.*)<\/des>/)[1];
    let link = content.match(/<url>(.*)<\/url>/)[1];
    

这里有东西没有匹配到,所以null[1]出错

'match' 函数没有匹配到会返回 'null',你需要先判断不是 'null' 再取值。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题