0

Update: I would really appreciate if anyone can point me in the right direction here. All of the links I've looked at and read through seem to be showing me how to sort documents within the console, but I still haven't worked out how to do this on the actual user interface. The code below shows how my index page is generating the 'blog' documents through a forEach loop. All I want to do is:

a) limit the number of results displayed on the app to 5 per page and b) sort these results by date

One user, as you can see, pointed me in the direction of another link regarding pagination,. I'm sure Im being stupid but I really am a beginner to all this and I cannot work out how this ties in with my code and how to implement this.


Original Post:

I'm creating a blog app using node.js and express with mongoDB as my database. All is up and running and working fine, however on the page which lists the blogs, I need to ensure that only a maximum of, say, 5 appear on each page and then I want to add pagination to scroll to older blogs. I've tried searching online how to do this but cannot find any explanation. Can anyone help?

Here the code which displays my blogs:

<div class = "container">
<div class = "jumbotron index">
    <div class="row">
        <div class="col-lg-2 col-sm-12">
            <h1><img id="icon" src = "https://www.seeklogo.net/wp-content/uploads/2014/10/logo-preview.png"> 
        </div> 
        <div class="col-lg-10 col-sm-12">
            <h1>Welcome to the Blog</h1>
            <div class="row">
                <div class="col-lg-12">
                    <h2>News, views and commentary</h2>
                </div>
            </div>
        </div>
    </div>    
    <!--<h2>News,  views and commentary on all things Toon!</h2>-->
</div>
<div class = "blogsList">
    <% blogs.forEach(function(blog){ %>
                <div class = "row">
                    <a class = "col-md-12" href = "/blogs/<%= blog._id %>"><h1><%= blog.title %></h1></a>
                </div> 
                <div class = "row meta">
                    <div class = "col-md-12 col-sm-12"><%= blog.created.toDateString() %></div>
                </div>
                <div class= "row">
                    <img class = "col-md-4 img-responsive" src="<%= blog.image %>">
                    <div class ="col-md-8"><% if(blog.body) { %><%-blog.body.substring(0,300)%>... <% } %></div>
                </div>
                <hr class = "style-two">
        <% }) %>
</div>
</div>
DevB1
  • 1,235
  • 3
  • 17
  • 43
  • And of course the very basic [`.skip()`](https://docs.mongodb.com/manual/reference/method/cursor.skip/) and [`.limit()`](https://docs.mongodb.com/manual/reference/method/cursor.limit/) query modifiers – Neil Lunn May 26 '17 at 11:50
  • Dude the great big box hovering over your question; [Implementing pagination in mongodb](https://stackoverflow.com/questions/28105009/implementing-pagination-in-mongodb) – Neil Lunn May 30 '17 at 11:54
  • Yep I've read that explantation through a thousand times and I still don't know how to implement this in my code within my forEach function. Anyway, can see you don't want to tell me (!!) ... let's leave it at that. Cheers anyway – DevB1 May 30 '17 at 12:03

0 Answers0