How to implement Mongoose-paginate in Node Express?

Asked

Viewed 890 times

0

I am trying to implement in my project Node Express paging in a menu list, and I found this documentation right below;

MONGOOSE-PAGINATE

According to the instructions I first installed lib with that command;

npm install mongoose-paginate

Then I set up my menu model including that line of code;

var mongoosePaginate = require('mongoose-paginate');

And this one too;

schema.plugin(mongoosePaginate);

As shown below;

'use strict'

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var mongoosePaginate = require('mongoose-paginate');

const schema = new Schema({
    id: {
        type: String,
        trim: true
    },
    name: {
        type: String,
        trim: true

    },
    description: {
        type: String,
        trim: true
    },
    restaurantId: {
        type: String,
        trim: true
    },
    price: {
        type: Number,
        trim: true
    },

})

schema.plugin(mongoosePaginate);
module.exports = mongoose.model('Menu', schema);

Then I created the method that will make the pagination this way below;

function getMenusPage (req, res) {
    Menus
    .find({ }, { page: 3, limit: 5 }, function(err, menu)  {
        if(err){
            res.status(500).send({
                message: 'Error na solicitação'
            });
        }else{
            if(!menu){
                res.status(404).send({
                    message: 'Não existe nenhum menu nesse registro'
                });
            }else{
                res.status(200).send({
                    menu
                });
            }
        }
    })
}

There is no error in the code or console, what I would like to know is how I will test in Postman to know if the implementation is correct?

I tried to do it that way

router.get('/menuspage', controller.getMenusPage);

With that URL;

http://localhost:3000/menuspage

But I made the mistake 404

1 answer

2


Good morning wladyband I would advise you to make the pagination with the module api-query-params It is very easy to use and you can easily adapt to the project. In addition to paging you can still make filters. Ex: do you mind the module

var aqp = require('api-query-params');

then imagine you want to list a complete collection. Ex: //List All Providers

exports.list_all_dataProviders = async (req, res) => {

  const { filter, skip, limit, sort, projection } = aqp(req.query);
  Provider
    .find(filter)
    .skip(skip)
    .limit(limit)
    .sort(sort)
    .select(projection)
    .exec(async (err, result) => {

      if (err) {
        return res.status(500).jsonp({message:"There was an internal error listing all the providers " + err});
      }

      let count = await Provider.find().count()


      res.status(200).jsonp({
        limit: limit,
        skip: skip,
        total: count,
        data: result
      });
    });

};

for paging use Skip and limit. However I think what you are missing if you want to keep Mongoose paginate is to call the method so.

var Menu= mongoose.model('Menu',  schema);
Menu.paginate();

and there you must have access to the available methods.

Mongoose-paginate-npm I hope it has helped , some doubt disposes

  • thank you very much caught, thanks very much, this is a find! hahahahaha, THANK YOU.

  • If there’s one more detail, he brought the date and total, but he didn’t bring the limit and the skip , i need this two to complete the implementation I will do on the Front-end

  • I guess you can see why he didn’t.

  • And just put it like that and have access http://localhost:3000/menuspage?skip=1&limit=4 I really liked that lib.

  • 1

    yes and that. And you can still use filters as you want a list only with names if you put names in filter names it will list all names etc :D

  • Ball show kkkk

  • If you don’t mind I would like to ask you one more question! what would the method look like if I were to perform a filter like this? http://localhost:3000/menu?description=Coberto, I mentioned Coberto, but could be any value in consultation

  • To filter you can use several forms: For example if you want a field of the json object you can use Projections: and you query it like this : http://localhost:3000/menu?fields=coberto or if you want to make a filter you can do so , in this example you have all the fields you can use status=sent&timestamp>2016-01-01&author.firstName=/john/i*&limit=100&skip=50&sort=-timestamp&fields=id' since the status up to the /i is applied the filter. I hope to have helped!

  • is that even with the guidelines with Projections or Filter I did not succeed.

  • they have to be implemented in order. See this example that was implemented by me. It’s in this api! However, I advise you to always use a limit because it is a lot of data to return. Not in the dataProviders part but in the Theses. https://masterplat.herokuapp.com/api-docs/

  • 1

    I found that if I do so the method behaves the way I want it http://localhost:3000/menu?description=/coberto/i , with that I solved in parts my problem.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.