Skip to content

"limit" Paging error when using "Schema" #68

@wesias7

Description

@wesias7
const querySchema = new QuerymenSchema({
  keywords: { search: true },
  joinedPlatform: { type: String, paths:['joined'], elementMatch: 'platform' },
  joinedGroup: { type: String, paths:['joined'], elementMatch: 'group' },
  joinedGrade: { type: String, paths:['joined'], elementMatch: 'grade' },
  createdAtDate: { type: Date, paths: ['createdAt'], duration: 'date', },
  status: status.type, 
  isAdult: status.isAdult, 
  activatedToEmail: activatedToEmail.type,
  activatedToMobile: activatedToMobile.type,
  activatedToIdentify: activatedToIdentify.type,
  activatedToBankAccount: activatedToBankAccount.type,
  allowMailing: allowMailing.type,
  allowSms: allowSms.type,
  page: { max: 100000 },
  limit: { max: 100000 }
})
querySchema.parser('search', (search, value, path, operator) => {
  if (!value) { return value }
  const regValue = new RegExp(value, "ig")
  if (search) {
    if(value*1 >= 0) {
      value = { $or: [{ mobile: regValue },{ accountNo: value*1 }] }
    } else {
      value = { $or: [{ email: regValue },{ name: regValue },{ realName: regValue },{ mobile: regValue },{ accountId: regValue }] }
    }
  }
  return value
})
querySchema.parser('elementMatch', (elementMatch, value, path, operator) => {
  if (!value) { return value }
  if (elementMatch) {
    value = { [path]: { $elemMatch: { [elementMatch]: value }, } }
  }
  return value
})
querySchema.parser('duration', (duration, value, path, operator) => {
  if (!value) { return value }
  if (duration == 'date') { 
    value = { [path]: { $gte: new Date(value).setHours(0,0,0,0), $lte: new Date(value).setHours(23,59,59,999), } }
  }
  return value
})
router.get('/',
  //token({ required: true, roles: ['admin'] }),
  query(querySchema),
  index)

then vscode debugs I saw an error like this.

{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 1360.531 ms - -
{ query: {},
  select: {},
  cursor: { skip: 60, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 25.560 ms - -
{ query: {},
  select: {},
  cursor: { skip: 0, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=1 200 27.345 ms - -
{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 27.026 ms - -
{ query: {},
  select: {},
  cursor: { skip: 60, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 26.904 ms - -
{ query: {},
  select: {},
  cursor: { skip: 90, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=4 200 30.536 ms - -
{ query: {},
  select: {},
  cursor: { skip: 120, limit: 15, sort: { createdAt: -1 } } }

Why is it an error here?

in querymen/index.js

    if (schema && schema.options && schema.options.near) {
      _schema = schema instanceof Schema
        ? _.clone(schema)
        : new Schema(schema, options)
    } else {
      _schema = schema instanceof Schema
        ? _.cloneDeep()
        : new Schema(schema, options)
    }

after

export function middleware (schema, options) {
  return function (req, res, next) {
    let _schema = schema instanceof Schema ? schema : new Schema(schema, options)

    _schema.validate(req.query, (err) => {
      if (err) {
        req.querymen = { error: err }
        res.status(400)
        return next(err.message)
      }

      req.querymen = _schema.parse()
      req.querymen.schema = _schema
      next()
    })
  }
}

This is how it works.

GET /?limit=15&page=3 200 27.436 ms - -
{ query: {},
  select: {},
  cursor: { skip: 0, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=1 200 24.773 ms - -
{ query: {},
  select: {},
  cursor: { skip: 15, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 25.139 ms - -
{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 24.126 ms - -
{ query: {},
  select: {},
  cursor: { skip: 45, limit: 15, sort: { createdAt: -1 } } }

Why did this happen?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions