Reputation: 11
i'm facing an error ' Unhandled promise rejections' when ever i run this code : `
app.get('/standings/:tableId', async (req, res) => {
const tableId = req.params.tableId
const tableAddress = tables.filter(table => table.id == tableId)[0].address
const team_ID = tables.filter(table => table.id == tableId) [0].id
const results = [];
try {
const result = await request.get(tableAddress);
const $ = cheerio.load(result);
$('#league-tables-wrapper > div > div.table-wrapper > table > tbody > tr').each((index, element) => {
const tds = $(element).find("td");
const rank = $(tds[0]).text();
const team = $(tds[2]).text();
const points = $(tds[10]).text();
const tableRow = { rank, team, points };
results.push({
rank,
team,
points
});
});
} catch (error) {
res.json(error);
}
res.json(results)
});
`
the error says that it is 'not possible to read property address of undefined' from this list : `
const tables = [
{
address: 'https://****/****/superliga',
id: 'Albania',
league: 'superliga'
},
{
address: 'https://****/****/ligue-1',
id: 'Algeria',
league: 'ligue 1'
},
` the resume of the error is : This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch() but i already have a catch statement.
can anyone tell me what is the issue with my code. i'm using express, cheerio and request-promise
Upvotes: 0
Views: 30
Reputation: 127
I can see a few problems with your code, however, to answer your question exactly : your try ... catch
is useless in that context. What you actually need to write is :
app.get('/standings/:tableId')
.then((req,res) => {your_whole_code_here})
.catch(e => console.log(e));
if you want your async errors to be caught correctly.
Also try triple equality for your Id tables.filter(table => table.id === tableId)[0].address
and check that tableId
corresponds to the format of your table Ids, because your filter just returns an empty array in that case.
Upvotes: 1