0
I am trying to implement in my project Node Express paging in a menu list, and I found this documentation right below;
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
thank you very much caught, thanks very much, this is a find! hahahahaha, THANK YOU.
– wladyband
If there’s one more detail, he brought the date and total, but he didn’t bring the
limit
and theskip
, i need this two to complete the implementation I will do on the Front-end– wladyband
I guess you can see why he didn’t.
– wladyband
And just put it like that and have access
http://localhost:3000/menuspage?skip=1&limit=4
I really liked that lib.– wladyband
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
– Tiago Neiva
Ball show kkkk
– wladyband
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 mentionedCoberto
, but could be any value in consultation– wladyband
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 usestatus=sent×tamp>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!– Tiago Neiva
is that even with the guidelines with Projections or Filter I did not succeed.
– wladyband
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/
– Tiago Neiva
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.– wladyband