Javascript “Cannot read property 'length' of undefined” when checking a variable's length

前端 未结 7 2034
孤独总比滥情好
孤独总比滥情好 2020-12-30 00:56

I\'m building a node scraper that uses cheerio to parse the DOM. This is more or a vanilla javascript question though. At one part of

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 01:43

    As has been discussed elsewhere, the .length property reference is failing because theHref is undefined. However, be aware of any solution which involves comparing theHref to undefined, which is not a keyword in JavaScript and can be redefined.

    For a full discussion of checking for undefined variables, see Detecting an undefined object property and the first answer in particular.

提交回复
热议问题