Home > other >  TypeError: dom.getElementsByTagName is not a function Node.js
TypeError: dom.getElementsByTagName is not a function Node.js

Time:10-03

I'm just starting with HTML (I'm not a web dev) and web. I'm trying to get a list of tags in an html document but I get the TypeError: dom.getElementsByTagName is not a function. I make the get request with axios, then with cheerio I load the response.data into a html document on which I call the getElementsByTagName method.. Am I missing to load some module or what am I doing wrong?? As always many thanks for your help. Here's my method:

const db = require("../config/database");
const axios = require('axios');
const jsdom = require('jsdom');
const{JSDOM} = jsdom;
const cheerio = require('cheerio');
// const Route = require('../schemas/route');
const apiKey = process.env.API_KEY;

exports.ingest = async(req, res) => {

    const {url} = req.query;
    const response = await axios.get(url); // Property 'get' does not exist on type 'typeof import("/Volumes/ProjectsSSD/FixitServer/fixit_server_node/node_modules/axios/index")' but it does makje the request..??
    // console.log('response is: ', response);
    
    if (response.status == 200){
 
        const document = cheerio.load(response.data, {decodeEntities: true}, false).html();
        // const document = cheerio.load(response.data, {decodeEntities: false}, true);
        console.log(' #################### response.data is: \n\n ', document);


//         var tag = document.createElement("div"); // TypeError: document.createElement is not a function
// tag.innerHTML = str;

// var t = tag.getElementsByTagName("*");
// var ele = [];
// for (var i = 0; i < t.length; i  ) {
//   ele.push(t[i].tagName);
// }

// console.log('tags are: ', ele);

    const dom = new JSDOM(document);
    console.log(' @@@@@@@@@@@@@@@@@@@@ dom is: \n\n', dom);
    const tags = document.getElementsByTagName('*'); // TypeError: dom.getElementsByTagName is not a function
    console.log('tags are: ', tags);


} else{
        res.status(response.status).send(response.statusText);
    }
    


};

CodePudding user response:

dom.getElementsByTagName should be dom.window.document.getElementsByTagName.

  • Related