Anybod familiar with mongoose paginate? how to paginate and filter?

First of all… can you also filter with mongoose?

Anyway… my code has broke down after trying to modify the route by adding pagination from mongoose…

I went from this

const router = require("express").Router();
let part = require("../Models/parts-schema");

router.route("/").get((req, res) => {
  part.find()
    .then((part) => res.json(part))
    .catch((err) => res.status(400).json("error: + err"));
});
module.exports = router;

to this…

const router = require("express").Router();
let part = require("../Models/parts-schema");

router.route("/").get(async (req, res) => {
  const limit = parseInt(req.query.limit, 100) || 100;
  const page = parseInt(req.query.page, 1) || 1;
  const parts = await part.paginate({}, { limit, page });
  res.json(parts);
});

module.exports = router;

And now I can’t read anything on the front end…

  1. I am trying to add the .catch error, but somehow I am getting syntax errors and it breaks my backend connection and can’t test anything with Insomnia.

  2. My front end when I get to the page went blank and get console errors.

  3. Can I somehow add a global filter in the route as well?

The front errors I am getting are below, but it was working fine with my previous router code…

The frontend code is below… I haven’t changed anything, but I would suppose I need to add more things to work with pagination and filtering…

import React, { Component } from "react";
import { Link } from "react-router-dom";
import axios from "axios";

const PartList = (props) => (
  <tr>
    <td>{props.part.Reference}</td>
    <td>{props.part.Description}</td>
    <td>{props.part.Replacements}</td>
  </tr>
);

export default class TableList extends Component {
  constructor(props) {
    super(props);

    this.state = { part: [] };
  }

  componentDidMount() {
    axios
      .get("http://localhost:5000/part/")
      .then((response) => {
        this.setState({ part: response.data });
      })
      .catch((error) => {
        console.log(error);
      });
  }

  partMapping() {
    return this.state.part.map((currentPart) => {
      return <PartList part={currentPart} key={currentPart._id} />;
    });
  }

  render() {
    return (
      <div>
        <h1>I am the table list component</h1>;
        <table>
          <thead>
            <tr>
              <th>Reference</th>
              <th>Description</th>
              <th>Replacement</th>
            </tr>
          </thead>
          <tbody>{this.partMapping()}</tbody>
        </table>
      </div>
    );
  }
}

Yes, I added key={currentPart._id} to the mapping… still get a broken code with the new router code and can’t see anything on the frontend.

And yes… I also added mongoose paginate…

const mongoose = require("mongoose");
const mongoosePaginate = require("mongoose-paginate-v2");
mongoose.pluralize(null);
const Schema = mongoose.Schema;

const partSchema = new Schema({
  Reference: { type: String },
  Description: { type: String },
  Replacements: { type: String },
});

partSchema.plugin(mongoosePaginate);

const part = mongoose.model("part", partSchema);

module.exports = part;

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.