Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

paging react mongoose

//server

// An example of a controller function for Express.js
async getAllUser (req, res) {
    try {
      const limit = parseInt(req.query.limit); // Make sure to parse the limit to number
      const skip = parseInt(req.query.skip);// Make sure to parse the skip to number

      // We are using the '3 layer' architecture explored on the 'bulletproof node.js architecture'
      // Basically, it's just a class where we have our business logic
      const userService = new userService();
      const users = await userService.getAll(limit, skip);

      return res.status(200).json(users);
    } catch(e){
      return res.status(500).json(e)
    }
},

class UserPaginationExample {
    getAll(limit = 0, skip = 0) {
        return UsersModel.aggregate([
            { $match: {} },    // This is your query
            { $skip: skip },   // Always apply 'skip' before 'limit'
            { $limit: limit }, // This is your 'page size'
        ])  
    }
}



//client

import React, { useState, useEffect } from 'react'

const fetchUsers = (limit, skip) => {
    // Make sure you send 'limit' and 'skip' as query parameters to your node.js server
    fetch(`/api/users?limit=${limit}&skip=${skip}`) 
        .then((res) => {
            this.setState({
                users: res.data;
            })
        })
}

const userList = () => {

    const [users, setUsers] = useState([]);
    const [limit, setLimit] = useState(20);
    const [skip, setSkip] = useState(0);

    const nextPage = () => {
        setSkip(skip + limit)
    }

    const previousPage = () => {
        setSkip(skip - limit)
    }

    useEffect(() => {
        fetchUsers(limit, skip)
    }, [skip, limit])


    return (<div> 
        <div> 
            { 
                users.map(user => 
                    <div> 
                        <span> { user.name } </span>
                        <span> { user.email } </span>
                        <span> { user.lastLogin } </span>
                    </div>
                )
            }
        </div>
        <div> 
            <div onClick={nextPage}> Previous Page </div>
            <div onClick={previousPage}> Next Page </div> 
        </div>
    </div>)
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: A simple javascript qr code generator 
Javascript :: Back button directive Angular 
Javascript :: number++ * 5 
Javascript :: use recursation in iife in js 
Javascript :: 1.047222170078745 
Javascript :: textfield label language react 
Javascript :: divcontainer is null 
Javascript :: javascript tact template 
Javascript :: javascript zoom to meters 
Javascript :: jquery listen for click on class that was created later 
Javascript :: setstate before function react 
Javascript :: hot add value in javascript 
Javascript :: setstate to false after 10 sec react native 
Javascript :: expo draw over other apps 
Javascript :: change color of input if submit clicked and input is empty 
Javascript :: express orm 
Javascript :: Set initial state on react-native-router-flex 
Javascript :: How To Add Google Social Login Button 
Javascript :: how to clear form fields in react after submit 
Javascript :: url builder angularjs 
Javascript :: connection string in static class 
Javascript :: how to log message with replacing placeholders in the string in js 
Javascript :: announcement for all server bot is in 
Javascript :: ternary operator online 
Javascript :: encrypt & decrypt api data in localstorage 
Javascript :: angular copy folder to dist 
Javascript :: Upload literal unsupported graphql 
Javascript :: go over each line in text nodejs 
Javascript :: how to allow the onclick event of a string in javascript 
Javascript :: how to combine all array element 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =