Here is my GET request handler, to render a page:
app.get("/:band", async (req, res) => {
const page = req.params.band;
try {
const result = await db.query("SELECT * FROM artists");
const artistData = result.rows;
const band = artistData.findIndex((info) => info.onewordid === page);
const bandInfo = artistData[band];
console.log(bandInfo);
const articlesresult = await db.query("SELECT * FROM articles WHERE artist_id = $1",
[bandInfo.id]);
res.render("artistpage.ejs", {
artistinfo: bandInfo,
data: artistData,
articles: articlesresult.rows,
});
} catch(err){
console.log(err)
}
});
An example band object:
{
id: 1,
name: 'The Rolling Stones',
picadress: 'backgroundpic2.jpeg',
onewordid: 'stones'
}
The error/output:
undefined
TypeError: Cannot read properties of undefined (reading 'id')
at file:///Users/admin/Downloads/Web%20development%20Project/Backend/Blog%20Project%20Styling/index.js:83:15
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Why can I console log this, but I get bandInfo.id as "undefined"?
Try
bandInfo[0].idinstead ofbandInfo.idin the second query.