第一次发表文章,不严谨处还请谅解!
今天在使用小程序解析网络传输过来的XML文件的时候,有遇到以下报错:
**WAServiceMainContext.js:2 TypeError: Cannot read property 'nodeValue' of null at Ni.getsourlist (<anonymous>:47:45) at Function.<anonymous> (<anonymous>:80:13) at :43035/appservice/<setTimeout callback function> at WASubContext.js?t=wechat&s=1641881332750&v=2.21.2:2 at a.<anonymous> (WASubContext.js?t=wechat&s=1641881332750&v=2.21.2:2) at Function.<anonymous> (WASubContext.js?t=wechat&s=1641881332750&v=2.21.2:2) at c (WAServiceMainContext.js:2) at WAServiceMainContext.js:2 at WAServiceMainContext.js:2(env: Windows,mp,1.05.2111300; lib: 2.21.2)**
期间尝试各种测试方法,均以失败告终,以下为参考案例:
通过打印XML解析出的数据,发现在某个子函数中不存在“firstChild”子函数;
通过进一步查看 数据传输的内容,发现传输的“guid”标签中内容为空;
没办法,百度吧,幸而搜索一个有用的函数 hasOwnProperty();
参考文章地址:https://blog.csdn.net/a791226606/article/details/110679991
if(guidlist[i].hasOwnProperty('firstChild')){
guiditem=guidlist[i].firstChild.nodeValue;
}
通过判断是否存在子函数,来规避报错的点;
问题得以解决!
【好文共赏析!】