这是我的 GET 请求,用于呈现页面:
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)
}
});
{ 编号: 1, 名称:“滚石乐队”, picadress: 'backgroundpic2.jpeg', onewordid: '石头' } 不明确的 类型错误:无法读取未定义的属性(读取“id”) 在文件:///Users/admin/Downloads/Web%20development%20Project/Backend/Blog%20Project%20Styling/index.js:83:15 在 process.processTicksAndRejections (节点:内部/进程/task_queues:95:5)
`
why can I console log this, but I get bandInfo.id as "undefined"?
PS sorry for messy code
在第二个查询中尝试
bandInfo[0].id
。