I’m encountering issues with pagination using mongoose-paginate-v2 in my TypeScript application. When I attempt to paginate my OfflineMacroAction collection, I’m not receiving any results despite expecting data to be present.
IOfflineMacroAction.ts
import { Document } from 'mongoose';
export default interface IOfflineMacroAction extends Document {
name: string
order: number
is_child: boolean
created_at: Date
updated_at: Date
deleted_at: Date
}
offlineMacroAction.ts
import IOfflineMacroAction from "../Interfaces/IOfflineMacroAction";
import mongoose, { Schema } from "mongoose";
import paginate from 'mongoose-paginate-v2';
const offlineMacroActionSchema: Schema = new Schema({
name: {
type: String,
required: true
},
is_child: {
type: Boolean,
default: false
},
order: {
type: Number,
default: 100
},
deletedAt: {
type: Date,
default: null // Optional: Set a default value for the deletedAt field
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
offlineMacroActionSchema.virtual('macroaction', {
ref: 'MacroAction',
localField: 'macroaction_id', // Of post collection
foreignField: '_id', // Of user collection
justOne: true
})
offlineMacroActionSchema.plugin(paginate);
export default mongoose.model<IOfflineMacroAction, mongoose.PaginateModel<IOfflineMacroAction>>('IOfflineMacroAction', offlineMacroActionSchema, 'offlineMacroActions');
and this is how i am calling it.
OfflineMacroAction.paginate({}, options, function (err: any, result: any) {
if (err) {
console.log(err);
} else {
// Here you will get paginate array please console and check
console.log(result);
}
});
output
{
docs: [],
totalDocs: 0,
limit: 10,
totalPages: 1,
page: 1,
pagingCounter: 1,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null
}
As I have data in in offlinemacroactions collection. Thanks in advance.