在向我的架构中添加插件后,在控制器中使用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);
谢谢,
答案 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);