S
S
Sergey2017-05-13 18:22:34
JavaScript
Sergey, 2017-05-13 18:22:34

How to output MongoDB records to a page?

I have a books collection in MongoDB with fields like this:

title: title,
    description: description,
    author: author,
    bookSource: bookSource,
    imageSource: imageSource

I want to display all the records (books) on the page. I am using ejs engine. Find all results:
var all_books = Book.find();
// и сгенерирую, передав результат
res.render("booksPage", { all_books: all_books });

I will make a for-loop on the ejs page:
<div class="booksBox">
<% for (var i = 0; i < all_books.length; i++) { %>
<div class="book">
<p class="title"> <%= all_books[i]["title"] %> </p>
<p class="description"> <%= all_books[i]["description"] %> </p>
<p class="author"> <%= all_books[i]["author"] %> </p>
<!-- И так далее... -->
</div>
<% } //конец for %>
</div>

It is right? How to do it right?
How can I display all books page by page, for example 10 books per page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2017-05-13
@vazonov

The code is correct. Using for on the ejs page (see the code above) you can display all the records. The issue with pagination has not yet been resolved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question