Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.paginate is not a function #135

Open
juanzone opened this issue Jan 30, 2018 · 0 comments
Open

model.paginate is not a function #135

juanzone opened this issue Jan 30, 2018 · 0 comments

Comments

@juanzone
Copy link

juanzone commented Jan 30, 2018

Hi guys,

I got a problem with this plugin.

When i run it, it says that is not a function..

Here is my code :

`const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate');
const Schema = mongoose.Schema;
const Tag = require('./tags');

const postSchema = new Schema({
title : String,
author : String,
body : String,
created : {type : Date, default: Date.now},
publish : Boolean,
tags: [{type : Schema.Types.ObjectId, ref : 'Tag' }]
});

postSchema.plugin(mongoosePaginate);

postSchema.statics = {

search : async function(params){
    const author = params.author;
    const title = params.title;
    const createdAt = params.createdAt;
    const publish = params.publish;
    const tag = params.tag;
    const currentPage = params.page || 1;
    var currentLimit = params.limit || 5;
    const sort = params.sort;

    let query = this.find()

    if(author){
        query.where('author', author)
    }

    if(title){
        query.where('title', title)
    }

    if(createdAt){
        query.where('created', createdAt)
    }

    if(publish){
        query.where('publish', publish)
    }
    
    
    if(tag){
        const data = await Tag.find({tagName : tag});
        query.where('tags', data[0]._id)
    }

    if(sort){
        this.sortPosts(sort);
    }else{
        query.sort({created : -1})
    }
    
    query.populate('tags')

    // Here is the problem i think.. 
    const results = await this.paginate(query, {page : 1, limit : 10});

    return results

},

sortPosts : async function(sort){
    var sortArray = sort.split(',');
    const sortField = sortArray[0];
    const sortValue = sortArray[1];
    switch (sortField){
        case 'title' : 
            return query.sort({title : sortValue});
            break;

        case 'author' : 
            return query.sort({author : sortValue})
            break;

        case 'creationDate' : 
            return query.sort({created : sortValue})
            break;

        default:
            return query.sort({created : -1})
    }
}

}

const Post = mongoose.model('Post', postSchema);

module.exports = Post;
`

Mongoose version :
"mongoose": "^5.0.2",
"mongoose-paginate": "^5.0.3",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant