服务器端的猫鼬分页

编程入门 行业动态 更新时间:2024-10-28 08:31:30
本文介绍了服务器端的猫鼬分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将服务器端分页添加到NodeJS,Express和MongoDB API.该API使用猫鼬来处理数据库.我不知道如何自定义Controller的响应.

I am trying to add server side pagination to a NodeJS, Express and MongoDB API. The API use mongoose to handle the database. I am lost in how to customize the response from the Controller.

型号:

const mongoose = require('mongoose'); const Schema = mongoose.Schema; const clientSchema = Schema({ code: { type: String, required: [true,'Code no puede estar vacio'] }, name: { type: String, required: [true,'Name no puede estar vacio'] } },{ timestamps: true }); const Client = module.exports = mongoose.model('clients',clientSchema);

用于获取所有客户的控制器:

Controller for get all clients:

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); }); });

如何在控制器中自定义结果,如下所示:

How can I customize the result in the controller to something like this:

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

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

I already have a function to calculate the pagination, But I don't know how to add the information about the pagination in the result of the controller.

在路由中添加分页数据之前,但是我想处理控制器中的分页逻辑.

Before I was adding the pagination data in the route, But I want to handle the pagination logic in the controller.

还是更好地处理路线中的分页?

Or is better handle the pagination in the route?

预先感谢

推荐答案

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

You can create a method in mongoose model called as paginate :

在声明猫鼬模型之前添加以下内容:

Add this before declaring mongoose model :

clientSchema.methods.paginate = function(pageNo, callback){ var limit = 10; var skip = pageNo * (limit - 1); var totalCount; //count documents this.count({}, function(err, count)){ if(err){ totalCount = 0; } else{ totalCount = count; } } if(totalCount == 0){ return callback('No Document in Database..', null); } //get paginated documents this.find().skip(skip).limit(limit).exec(function(err, docs){ if(err){ return callback('Error Occured', null); } else if(!docs){ return callback('Docs Not Found', null); } else{ var result = { "totalRecords" : totalCount, "page": pageNo, "nextPage": pageNo + 1, "result": docs }; return callback(null, result); } }); }); const Client = module.exports = mongoose.model('clients',clientSchema);

然后更改控制器:

app.get('/api/clients', (req, res) => { //You could put page number in request query ro request params Client.paginate(req.body.pageNo, function(err, response) { if (err) { return res.status(500).json({ message : "Error en aplicacion", error : err }); } return res.status(200).json(response); });

更多推荐

服务器端的猫鼬分页

本文发布于:2023-11-22 23:49:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619380.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   服务器端

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!