So this piece of code is returning description of word in json from https://sjp.pl/ webiste, word which user provied before in request json body
And problem is that i want to send back var 'desc' in res.send but its undefined. How I can access that and send it?
I think thats because item.push($(this).text()) is working only in this axios function and can be accessed only in this scope
const item = [];
var desc;
app.post('/test', (req, res) => {
const { word } = req.body;
if(!word) {
res.status(418).send({error: 'no word provided'})
}
console.log(word)
axios(`https://sjp.pl/${word}`).then(res => {
const html = res.data
const $ = cheerio.load(html)
$('p').each(function() {
item.push($(this).text())
})
desc = item[3]
console.log(desc) // working until here
})
console.log(desc) // undefined here
res.send({
description: `${desc}`
})
})