Partners help me! I want to paginate the results of my Artist model extracted from mongoDB but the following error appears:
Artist.find(...).sort(...).paginate is not a function
This is my model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ArtistSchema = new Schema({
name: String,
description: String,
email: String,
image: String,
});
module.exports = mongoose.model('Artist', ArtistSchema);
This is the function:
const mongoosePagination = require('mongoose-pagination');
const Artist = require('../models/artist');
function getArtists(req, res) {
let page = 1;
const itemsPerPage = 3;
if (req.params.page) {
page = req.params.page;
}
Artist.find().sort('name').paginate(page, itemsPerPage, (err, artists, total) => {
if (err) return res.status(500).send({ message: 'Error in the request' });
if (!artists) return res.status(404).send({ message: 'No artists' });
return res.status(200).send({
artists,
totalItems: total,
});
});
}
This is the route:
const ArtistController = require('../controllers/artist');
const router = express.Router();
router.get('/artists/:page?', ArtistController.getArtists);
module.exports = router;
Please help me!