6

I have a problem using paginate in my controller after add plugin to my schema, my code is written in TypeScript 2.1, i have installed @types/mongoose-paginate in devdependencies.

[ts] severity: 'Error' message: 'Property 'paginate' does not exist on type 'Model'.'

My Controller:

export function getAllArtists(req, res) {
Artist.paginate({}, { page: 3, limit: 10 }, function(err, result) {
    // ...
    // ...
});

My Schema:

'use strict'
import {Document, model, Model, Schema} from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate';

interface IArtist extends Document {
    name: String;
    description: String;
    image: String;
}

const ArtistSchema: Schema = new Schema({
    name: String,
    description: String,
    image: String
});

ArtistSchema.plugin(mongoosePaginate);

export const ArtistModel: Model<IArtist> = model<IArtist>('Artist', ArtistSchema);

Thanks,

1 Answers1

10

The solution is include a interface with extend the PaginateModel to my Schema.

'use strict'

import { PaginateModel, Document, Schema, model } from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate';

interface IArtist extends Document {
    name: String;
    description: String;
    image: String;
}

const ArtistSchema: Schema = new Schema({
    name: String,
    description: String,
    image: String
});

ArtistSchema.plugin(mongoosePaginate);

interface ArtistModel<T extends Document> extends PaginateModel<T> {}

export const ArtistModel: ArtistModel<IArtist> = model<IArtist>('Artist', ArtistSchema);