create account

Firebase firestore in the forum application #18: Paginated system for forums and comments by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap ·
$14.88
Firebase firestore in the forum application #18: Paginated system for forums and comments
#### Repository
https://github.com/firebase

#### What Will I Learn?
- Paginated system for forums and comments

#### Requirements
- Basic Javascript
- Install Firebase


#### Resources
- Firebase-https://firebase.google.com/

#### Difficulty
Basic

### Tutorial Content

Hi everyone, in this tutorial, we will still be developing forum applications using firebase, this tutorial has developed every series of tutorials, now this time I will make one of the newest features of the following tutorial. So for those of you who are just following this tutorial, I suggest you follow the curriculum below, in this tutorial we will learn how to create a pagination system using the firebase database. we just start this tutorial.

### Paginated system

Here, in general, we will learn how to manage how to display data with a horizontal database type. if you have used the Firestore database, you must have heard the database *horizontally*. If you previously knew ***MySql*** or other database drivers you would understand the tables and columns, but not with the firebase database we don't know that term. The database in Firebase is a type of ***NoSql*** database, this database has a different structure from ***MySql***. In NoSql we have a database *structure* with ***documents*** and ***collections***.

- **Create system pagination**

We already know what ***MySql*** and ***NoSql ***, Now we will start creating the system the paginations on our forum application, especially in the comments feature. as we know comments are very important features in interaction on social media applications. A post will have tens or even thousands of comments, then how do we maximize the comment so that it can be seen properly by the user. Well, here we will solve the problem. we can see how far the comments feature that we have made on our forum application, we can see in the picture below:

![Screenshot_9.png](https://ipfs.busy.org/ipfs/Qme7RaoiptPQQ8fFVQsUs6wc9JDVqzehPGdno2DvEEYi6L)

We can see in the picture above we still have a few comments, imagine if you have a lot of comments like we can see in the picture below:


![ezgif.com-video-to-gif (5).gif](https://cdn.steemitimages.com/DQmZqcz5p7djPDujDgLbaZHi2J51ymWXSuacwMRCKWiueGj/ezgif.com-video-to-gif%20(5).gif)


![Screenshot_10.png](https://ipfs.busy.org/ipfs/QmSLAP47Zg6TLeJ8if4Au4UV7VbJJWNyYTNycyszorRtPe)


Well now we can see in the picture above we have 4 comments that we will use to make pagination and also in our database, we can see our database has 4 documents in the replies ***subcollection***.

### Make the pagination function

Here we will begin to understand and make the pagination system on the Firestore database, The first step that we will start is to provide a limit for the forum that we will display, here I will create a ***limit of 2*** forum data. To save the data.

- **Paginatied forums**

It can start from the path ```'/ forum'```. Because the forum data that we will display is located in the **/ forum**. We will start pagination on the forum which is the main collection of the database that we created, we can use the ```limit ()``` function provided by firebase. because firebase doesn't use MySql so we can use the functions provided by Firebase. For more details, we can see in the picture below:

**functions/index.js**

```
app.get('/forum', function(req, res) {
var forums = [];
	db.collection('forums').orderBy('updated_at', 'desc').limit(2).get()
	.then(snapshot => {
		snapshot.forEach(doc => {
			forums.push(doc.data())
		})
		res.render('home', {dataForums:forums})
	}).catch(err => {
		console.log(err)
	})
})
```

The code is a query to retrieve forum data in our database, we have finished it in the previous tutorial, for more details you can see this [tutorial](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-6-forums-collection-and-add-data-in-collection-1559196649948). I will give limit 2 ```.limit(2)``` for the collection of forums that we use in this application.

![ezgif.com-video-to-gif (1).gif](https://cdn.steemitimages.com/DQmRM35kbcEGUpBWXa5ZE26RxtshEXZ5KefvQNFqr35Y9af/ezgif.com-video-to-gif%20(1).gif)

We can see in the picture above that we have succeeded in paginating the forum collections that we have, we can limit the applications we use, then we will create a paginated system in the comments.
<br>
- **Paginated comments**
It can start from the path ```'/ forum/:slug'```. Because the forum data that we will display is located in the '/ forum/:slug. The ***routing*** application that we have created is in the backend cloud function located at ***functions/index.js***. For more details, we can see the code below:

```
app.get('/forum/:slug', function(req, res) {
var forum = null;
	db.collection('forums').where('slug', '==', req.params.slug).get()
	.then(snapshot => {
		snapshot.forEach(doc => {
			forum = doc.data()
			forum.id = doc.id
		})

		// Load replies
		var replies=[]
		db.collection('forums').doc(forum.id).collection('replies').limit(2).get()
			.then(snapshot => {
				snapshot.forEach(doc => {
					replies.push({
						data: doc.data(),
						id:doc.id
					})
				})
				res.render('forum-detail', {forum:forum, replies:replies})
			}).catch(function(err) {
				console.log("There is something wrong")
			})
	}).catch(err => {
		console.log(err)
	})
});
```
The code above is the path of ```/forum/:slug```, unlike ***MySql*** in **Firebase** we use a function that has been provided by firebase, to do the limit we can use the ```limit ()``` and the parameter is the amount to be copied in the code above I will ```limit(2)```. For the results we can see in the picture below:

![ezgif.com-video-to-gif.gif](https://cdn.steemitimages.com/DQmNQdyYE1pzhd5s99zY7L7iv8HhQDrg1oRGYibZb7L2dGd/ezgif.com-video-to-gif.gif)

We can see we have also succeeded in creating a pagination system on the applications we use. we can see in the picture above we have limited the comments.
<br>
- **Create button for interface**

To make an interface button next and previous, we must use a marker to retrieve the last data so that it can be used to retrieve the next or previous data, we can see the example in the code below:

**functions/index.js**

```
app.get('/forum', function(req, res) {
var forums = [];
	db.collection('forums').orderBy('updated_at', 'desc').limit(1).get()
	.then(snapshot => {
		snapshot.forEach(doc => {
			forums.push(doc.data())
		})
		var lastItem = forums[forums.length - 1 ];
		res.render('home', {dataForums:forums, lastForumTime: Date.parse(lastItem.updated_at)})
	}).catch(err => {
		console.log(err)
	})
})
```
Well in this code we will pass the last time from the data in the forum, to get it we can use this method ```var lastItem = forums[forums.length - 1 ];```. We can take the ***index*** value that we have **reduced -1**.

Then we can ***render*** it by passing it to the **home.hbs** template as follows ```res.render('home', {dataForums:forums, lastForumTime: Date.parse(lastItem.updated_at)})```. Because the form of time is still in ```Y-m-d``` then we must *pass* it to the **timestamp** first in this way ```Date.parse(lastItem.updated_at)```. we passed the time with key  ```lastForumTime```

And now we can switch to the **home.hbs** template whether the data was successfully rendered properly:

![Screenshot_11.png](https://ipfs.busy.org/ipfs/QmckWj354zT16UrDctAmaaJBJP7JXeo3iontRnTrqvKQGR)
Well we can see in the picture above we have succeeded in rendering the last time from the data forums that we have. Thank you for following this tutorial, hopefully, it will be useful for you


### Curriculum

- **Forum app**

[Firebase app#1](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-1-firebase-init-and-cloud-backend-function-1557758217688), [Firebase app#2](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-2-local-server-routing-and-template-login-gmail), [Firebase app#3](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-3-logout-function-and-login-with-facebook-account), [Firebase app#4](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-3-logout-function-and-login-with-facebook-account), [Firebase app#5](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-4-create-apps-and-login-with-a-twitter-account-1558448308709), [Firebase app#6](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-6-forums-collection-and-add-data-in-collection-1559196649948), [Firebase app#7](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-7-store-user-data-in-forums-and-firebase-admin-1559372598705), [Firebase app#8](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-8-fetch-data-from-collection-and-render-data-cloud-function-1559462309843), [Firebase app#9](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-9-implement-slug-as-url-params-and-single-app-forum), [Firebase app#10](https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-10-authorized-domain-and-user-forum-ownership-1560338464527), 


#### Proof of work done

https://github.com/milleaduski/firebase-forum
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 28 others
πŸ‘Ž  
properties (23)
authorduski.harahap
permlinkfirebase-firestore-in-the-forum-application-18-paginated-system-for-forums-and-comments-1562476700306
categoryutopian-io
json_metadata{"app":"steeditor/0.1.2","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmckWj354zT16UrDctAmaaJBJP7JXeo3iontRnTrqvKQGR","https://ipfs.busy.org/ipfs/Qme7RaoiptPQQ8fFVQsUs6wc9JDVqzehPGdno2DvEEYi6L","https://cdn.steemitimages.com/DQmZqcz5p7djPDujDgLbaZHi2J51ymWXSuacwMRCKWiueGj/ezgif.com-video-to-gif%20(5","https://ipfs.busy.org/ipfs/QmSLAP47Zg6TLeJ8if4Au4UV7VbJJWNyYTNycyszorRtPe","https://cdn.steemitimages.com/DQmRM35kbcEGUpBWXa5ZE26RxtshEXZ5KefvQNFqr35Y9af/ezgif.com-video-to-gif%20(1","https://cdn.steemitimages.com/DQmNQdyYE1pzhd5s99zY7L7iv8HhQDrg1oRGYibZb7L2dGd/ezgif.com-video-to-gif.gif","https://ipfs.busy.org/ipfs/QmckWj354zT16UrDctAmaaJBJP7JXeo3iontRnTrqvKQGR"],"tags":["utopian-io","tutorials","firebase","web"],"users":["duski"],"links":["https://github.com/firebase","https://firebase.google.com/","https://cdn.steemitimages.com/DQmZqcz5p7djPDujDgLbaZHi2J51ymWXSuacwMRCKWiueGj/ezgif.com-video-to-gif%20","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-6-forums-collection-and-add-data-in-collection-1559196649948","https://cdn.steemitimages.com/DQmRM35kbcEGUpBWXa5ZE26RxtshEXZ5KefvQNFqr35Y9af/ezgif.com-video-to-gif%20","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-1-firebase-init-and-cloud-backend-function-1557758217688","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-2-local-server-routing-and-template-login-gmail","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-3-logout-function-and-login-with-facebook-account","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-4-create-apps-and-login-with-a-twitter-account-1558448308709","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-7-store-user-data-in-forums-and-firebase-admin-1559372598705","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-8-fetch-data-from-collection-and-render-data-cloud-function-1559462309843","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-9-implement-slug-as-url-params-and-single-app-forum","https://steemit.com/utopian-io/@duski.harahap/firebase-firestore-in-the-forum-application-10-authorized-domain-and-user-forum-ownership-1560338464527","https://github.com/milleaduski/firebase-forum"]}
created2019-07-07 05:18:24
last_update2019-07-07 05:18:24
depth0
children4
last_payout2019-07-14 05:18:24
cashout_time1969-12-31 23:59:59
total_payout_value11.261 HBD
curator_payout_value3.619 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,399
author_reputation60,094,717,098,672
root_title"Firebase firestore in the forum application #18: Paginated system for forums and comments"
beneficiaries
0.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id87,923,934
net_rshares37,714,400,755,707
author_curate_reward""
vote details (93)
@portugalcoin ·
$7.16
Thank you for your contribution @duski.harahap.
After reviewing your contribution, we suggest you following points:

- This tutorial is interesting and well structured and explained. Good job again!

- In the sections of your code put more comments. It helps a lot the comments in the code.

- Thanks for following some of our suggestions in your previous tutorials. 

Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/2-1-3-1-1-3-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkpu9qvc
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["duski.harahap"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/2-1-3-1-1-3-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-07-07 11:06:48
last_update2019-07-07 11:06:48
depth1
children1
last_payout2019-07-14 11:06:48
cashout_time1969-12-31 23:59:59
total_payout_value5.487 HBD
curator_payout_value1.668 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length917
author_reputation602,509,133,881,806
root_title"Firebase firestore in the forum application #18: Paginated system for forums and comments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id87,934,589
net_rshares17,365,906,468,335
author_curate_reward""
vote details (21)
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-pu9qvc-20190710t013011z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-07-10 01:30:12
last_update2019-07-10 01:30:12
depth2
children0
last_payout2019-07-17 01:30:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation152,955,367,999,756
root_title"Firebase firestore in the forum application #18: Paginated system for forums and comments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id88,091,328
net_rshares0
@steem-ua ·
#### Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-firebase-firestore-in-the-forum-application-18-paginated-system-for-forums-and-comments-1562476700306-20190707t111621z
categoryutopian-io
json_metadata"{"app": "beem/0.20.19"}"
created2019-07-07 11:16:24
last_update2019-07-07 11:16:24
depth1
children0
last_payout2019-07-14 11:16:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation23,214,230,978,060
root_title"Firebase firestore in the forum application #18: Paginated system for forums and comments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id87,934,859
net_rshares0
@utopian-io ·
Hey, @duski.harahap!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-firebase-firestore-in-the-forum-application-18-paginated-system-for-forums-and-comments-1562476700306-20190707t134137z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-07-07 13:41:39
last_update2019-07-07 13:41:39
depth1
children0
last_payout2019-07-14 13:41:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length595
author_reputation152,955,367,999,756
root_title"Firebase firestore in the forum application #18: Paginated system for forums and comments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id87,939,967
net_rshares0