在插件中导入模式后调用'paginate'时出错

时间:2017-03-17 14:11:09

标签: typescript mongoose mongoose-schema mongoose-plugins

在向我的架构中添加插件后,在控制器中使用paginate时遇到问题,我的代码是用TypeScript 2.1编写的,我在devdependencies中安装了@ types / mongoose-paginate。

[ts]严重程度:'错误'消息:“属性'分页''在类型'模型'上不存在。'

我的控制器:

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

我的架构:

'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);

谢谢,

1 个答案:

答案 0 :(得分:2)

解决方案是包含一个接口,将PaginateModel扩展到我的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);