0

I am very new to programming and I am attempting to select and then display the images for the 3 most recently created books in my database but when I sort by createdAt, the images will no longer display.

This is the ejs to display the images:

<% for(let r of recentBooks) { %>
                <img class="recentBooks cover" src=" <%= r.covers[0].url %>" alt="">
                <% } %>

This is the find version that displays the images, but not for the three most recent books:

app.get('/', async (req, res) => {
    const recentBooks = await Book.find().limit(3);
    console.log(recentBooks);
    res.render("home", { recentBooks })
})

This is the find version that give me a "Cannot read properties of undefined (reading 'url')" error:

app.get('/', async (req, res) => {
    const recentBooks = await Book.find().sort({ createdAt: -1 }).limit(3);
    console.log(recentBooks);
    res.render("home", { recentBooks })
})

Why is this happening and what can I do to fix it? Thank you

cnharmon
  • 3
  • 3

0 Answers0