Home > Back-end >  how to render next and previous pages , get the data from mongodb and using nodejs
how to render next and previous pages , get the data from mongodb and using nodejs

Time:12-25

I am making a blog website. where are stored some articles, I want next and previous data clicking by buttons. I do not understand, how to solve this. share some solutions and suggestions with me related to this. information... I am rendering all articles to the web page, where these articles are stored in a list with the title and getting one for reading with the whole content. i am using MongoDB and nodejs for template ejs

CodePudding user response:

I want next and previous data

Firstly, you need to determine what is "next" and what is "previous".

I assume you have some sorting on your MongoDB query? Perhaps you are sorting by date? Maybe you have a field set up for some sort of sort index/priority? Somehow, you need a deterministic ordering for your list.

See also: https://docs.mongodb.com/manual/reference/operator/aggregation/sort/

I am rendering all articles to the web page, where these articles are stored in a list with the title and getting one for reading with the whole content

I recommend linking to your blog articles by ID. So, when you load the data for one article, also get the IDs for the previous and next articles by querying MongoDB for them. Then in your template, you can make a regular anchor element:

<a href="/articles/<<article-id-goes-here>>">Next</a>

If you want to style them like buttons, you can use CSS to set a border or background:

a.button {
  border: 0.1em solid #777;
  background: #aaa;
}
  • Related