How to implement mongoose-paginate on Node Express?

0

I'm trying to implement in my Node Express paging project in a menu list, and I found this documentation low enough;

MONGOOSE-PAGINATE

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

npm install mongoose-paginate

Then I set up my model menu including this 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 paginate like this 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
                });
            }
        }
    })
}

You are not generating a bug in the code or console, what would you like to know is how will I test on Postman to know if the implementation is correct?

I tried to do it that way

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

With this URL;

http://localhost:3000/menuspage

But I had the error 404

    
asked by anonymous 16.08.2018 / 12:08

1 answer

2

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

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

Then imagine that 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 pagination you use skip and limit. However I think what you're missing if you want to keep the mongoose paginate is to call the method like this.

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

and then you should have access to the available methods.

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

    
16.08.2018 / 13:01