服务器端的猫鼬分页

时间:2017-05-11 21:15:19

标签: node.js mongodb express mongoose

我正在尝试将服务器端分页添加到NodeJS,Express和MongoDB API。

API使用mongoose来处理数据库。

我迷失了如何自定义Controller的响应。

型号:

const mongoose = require("mongoose");
const Client = require('../models/client');
const clientController = {};


clientController.index = (limit, callback) => {
  Client.find(callback).limit(limit);
};

module.exports = clientController;

获取所有客户的控制器:

  app.get('/api/clients', (req, res) => {
      Client.index(limit,(err, client) => {

        if (err) {
          res.status(500).json({
            msg: "Error en aplicacion",
            err
          });
        }
        res.status(200).json(client);
      });
  });

获取客户的路线:

[
{
"totalRecords":"99999999999",
"offset":"888888",
"page":"4",
"nextPage":"5"
"result":{...}
}
]

如何在控制器中自定义结果:

import pygame, sys
from pygame.locals import *

pygame.init()

DISPLAYSURF = pygame.display.set_mode((300,300))

def checkCollide(event):
    k = 0
    a,b = event.pos
    x = P1[0].get_rect()
    if x.collidepoint(a,b):
        return True
    return False

CP1 = [(150, 150)
      ,(155, 150)
      ,(160, 150)
      ,(165, 150)
      ,(170, 150)
      ,(175, 150)
      ,(180, 150)
      ,(185, 150)
      ,(190, 150)]

statp1_1 = 0

WHITE = (255,255,255)
DISPLAYSURF.fill(WHITE)

while True: # the main game loop
    P1 = [pygame.image.load('PAzul.png'),CP1[statp1_1],statp1_1]
    DISPLAYSURF.blit(P1[0], P1[1])
    e = pygame.event.get()
    for event in e:
        if event.type == MOUSEBUTTONUP:
            a = checkCollide(event)
            if a:
                DISPLAYSURF.fill(WHITE)
                statp1_1 +=1

        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

我已经有了计算分页的功能,但我不知道如何在控制器的结果中添加有关分页的信息。

在我在路线中添加分页数据之前,我想在控制器中处理分页逻辑。

或者更好地处理路线中的分页?

提前致谢

1 个答案:

答案 0 :(得分:2)

您可以在mongoose模型中创建一个名为paginate的方法:

在声明mongoose模型之前添加:

app.get('/api/clients', (req, res) => {
  Client.paginate(pageNo, function(err, response){

    if (err) {
      return res.status(500).json({
        message : "Error en aplicacion",
        error : err
      });
    }
    return res.status(200).json(response);
  });

然后在控制器更改中:

sum_cpp

});