Search code examples
angularionic4

Error while using Node html parser : error TS2339: does not exist on type '(TextNode & { valid: boolean; }) | (HTMLElement & { valid: boolean; })


I'm scraping the website for my project and for that I'm using axios and for the html parser using node-html-parser using this i'm getting this error while parsing the dom

  " Error TS2339: Property
    'querySelectorAll' does not exist on type '(TextNode & { valid: boolean; }) | (HTMLElement & { valid: boolean; })' "

and the code i wrote is :

axios.get('url',  config).then((res) => {
      var rawHtml = res.data; 
      const root = parse(rawHtml);
      // console.log(root.querySelectorAll('option')); //Error here
     root: HTMLElement   = root.removeWhitespace() ;  //Error
      const data    =  root.querySelectorAll('option') ;

in short wherever I'm using any method of node html parse I'm getting this error


Solution

  • I just ran into the problem the other day. This worked for me: try casting your created element as an HTMLElement.

    const root = parse(rawHtml) as HTMLElement;
    

    Now it should have query selector functions available to it without type errors.