I'm trying to use template literals to pass input data to a function as below and I don't have a clue why it's not working. Tried everything already. It simple does not enter the cheerio $
function when I use ${}
. If I write the exact word instead, it works.
module.exports = function scrap(keyword) {
sources.forEach(async source => {
axios.get(source.url).then(response => {
const html = response.data;
const $ = cheerio.load(html);
console.log("the keyword is " + keyword);
console.log("the source url is " + source.url)
console.log(typeof keyword);
console.log(`The keyword is "${keyword}"`);
$(`a:contains("${keyword}")`, html).each(function() {
console.log("entered")
const title = $(this).text();
const url = $(this).attr('href');
articles.push({
title,
url,
source: source.name,
});
});
}).catch(err => {
console.log(err);
});
console.log(articles)
});
return articles;
}
So let's say that if I try
$(`a:contains("bananas")`, html).each(function () { ...
it will work.
Any hints why template literals are not working?