aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

why there is a false field? #42

Closed bulolo closed 2 months ago

bulolo commented 2 years ago
{
  totalDocs: 'total',
  docs: 'docs',
  limit: 'limit',
  offset: 'offset',
  page: false,
  nextPage: false,
  prevPage: false,
  totalPages: false,
  pagingCounter: false,
  meta: false,
  hasPrevPage: false,
  hasNextPage: false,
}
image
aravindnc commented 2 years ago

@bulolo You should provide more info to debug.

bulolo commented 2 years ago

Schema

const mongoosePaginate = require('mongoose-paginate-v2')
const aggregatePaginate = require('mongoose-aggregate-paginate-v2')
module.exports = (app) => {
  const mongoose = app.mongoose
  const Schema = mongoose.Schema
  const ApplySchema = new Schema(
    {
      applySn: { type: String, required: true, unique: true }, 
      type: {
        type: String,
        enum: ['SCHEDULE', 'WARNING', 'MANUAL'], 
        required: true,
      },
      org: { type: String, required: true }, 
      creator: { type: Schema.Types.ObjectId, ref: 'User', required: true }, 
      creatorName: { type: String, required: true }, 
      created: { type: Date }, 
      updated: { type: Date }, 

      manufacturers: [
        {
          type: String,
          required: true,
        },
      ], 
      areas: [
        {
          type: String,
          enum: ['BJ', 'SH', 'GZ', 'SZ', 'CQ', 'SY', 'WH'],
          required: true,
        },
      ],
      totalItems: {
        type: Number,
        required: true,
      }, 
      totalQty: {
        type: Number,
        required: true,
      }, 
      status: {
        type: String,
        enum: ['PENDING', 'SECTIONED', 'CONFIRMED'],
        default: 'PENDING',
        required: true,
      }, 
    },
    {
      collection: 'sys_purchase_apply',
      timestamps: { createdAt: 'created', updatedAt: 'updated' }
    }
  )

  ApplySchema.plugin(mongoosePaginate)
  ApplySchema.plugin(aggregatePaginate)
  return mongoose.model('Apply', ApplySchema)
}

code

const { current, pageSize, type } = payload
    const match = {
      $match: {
        type: type,
      },
    }
    const aggregate = ctx.model.Purchase.Apply.aggregate([
      match
    ])
    const options = {
      lean: true,
      offset: pageSize * (current - 1),
      limit: pageSize,
      sort: { created: -1 },
      customLabels: {
        totalDocs: 'total',
        docs: 'docs',
        limit: 'limit',
        offset: 'offset',
        page: false,
        nextPage: false,
        prevPage: false,
        totalPages: false,
        pagingCounter: false,
        meta: false,
        hasPrevPage: false,
        hasNextPage: false,
      },
    }
    const res = await ctx.model.Purchase.Apply.aggregatePaginate(
      aggregate,
      options
    )
    return res

response

{
    "code": 0,
    "success": true,
    "data": {
        "docs": [
            {
                "_id": "62426cfa836dae632bc00f11",
                "manufacturers": [
                    "Biolegend"
                ],
                "areas": [
                    "SH",
                    "GZ",
                    "SZ"
                ],
                "status": "PENDING",
                "applySn": "APY202203291020421333",
                "type": "MANUAL",
                "creator": "5eba176654c70a2bc8df0719",
                "creatorName": "张云",
                "org": "DKWSH",
                "totalItems": 4,
                "totalQty": 9,
                "created": "2022-03-29T02:20:42.577Z",
                "updated": "2022-03-29T02:20:42.577Z",
                "__v": 0
            }
        ],
        "total": 1,
        "limit": 10,
        "false": null,
        "offset": 0
    },
    "msg": "请求成功"
}
image

@aravindnc

bulolo commented 2 years ago

@aravindnc any idea,is it a bug?

aravindnc commented 2 years ago

That's weird. I will take a look in detail.

aravindnc commented 2 years ago

@bulolo Remove below customLabels from your code and try again.

        page: false,
        nextPage: false,
        prevPage: false,
        totalPages: false,
        pagingCounter: false,
        meta: false,
        hasPrevPage: false,
        hasNextPage: false,

Do not pass customLabels if you are not using it.

bulolo commented 2 years ago
{
  totalDocs: 'total',
  docs: 'docs',
  limit: 'limit',
  offset: 'offset',
  // page: false,
  // nextPage: false,
  // prevPage: false,
  // totalPages: false,
  // pagingCounter: false,
  // meta: false,
  // hasPrevPage: false,
  // hasNextPage: false,
}
{
        "docs": [],
        "total": 0,
        "limit": 10,
        "page": 1,
        "totalPages": 1,
        "pagingCounter": 1,
        "hasPrevPage": false,
        "hasNextPage": false,
        "offset": 0,
        "prevPage": null,
        "nextPage": null
    }

@aravindnc

bulolo commented 2 years ago

i just want this 4 fields

  totalDocs: 'total',
  docs: 'docs',
  limit: 'limit',
  offset: 'offset',

like

{
        "docs": [],
        "total": 0,
        "limit": 10,
        "page": 1,
        "offset": 0,
    }

how to hide

        "page": 1,
        "totalPages": 1,
        "pagingCounter": 1,
        "hasPrevPage": false,
        "hasNextPage": false,
        "prevPage": null,
        "nextPage": null

@aravindnc

aravindnc commented 2 years ago

@bulolo Sorry there is no option to hide other fields as of now.