我对编程很陌生,我正在尝试选择并显示数据库中最近创建的 3 本书的图像,但是当我按 createdAt 排序时,图像将不再显示。
这是显示图像的ejs:
<% for(let r of recentBooks) { %>
<img class="recentBooks cover" src=" <%= r.covers[0].url %>" alt="">
<% } %>
这是显示图像的查找版本,但不适用于最近的三本书:
app.get('/', async (req, res) => {
const recentBooks = await Book.find().limit(3);
console.log(recentBooks);
res.render("home", { recentBooks })
})
这是给我一个“无法读取未定义的属性(读取'url')”错误的查找版本:
app.get('/', async (req, res) => {
const recentBooks = await Book.find().sort({ createdAt: -1 }).limit(3);
console.log(recentBooks);
res.render("home", { recentBooks })
})
为什么会发生这种情况,我该如何解决?谢谢