我是nodejs中的新手编码。我正在尝试创建分页,但出现错误。您能帮助解决这个问题吗?
这是index.js中的代码
router.get('/items/:page',(req,res) =>{
const db = require('mysql'), Pagination = require('./master_data'),
page_id = parseInt(req.params.page),
currentPage = page_id > 0 ? page_id : currentPage,
pageUri = '/items/';
db.query('SELECT COUNT(id) as totalCount FROM pendapatan',(err,result)=>{
const perPage = 10,
totalCount = result[0].totalCount;
const Paginate = new Pagination(totalCount,currentPage,pageUri,perPage);
db.query('SELECT * FROM pendapatan LIMIT '+Paginate.perPage+' OFFSET'+Paginate.start,(err,result)=>{
data = {
items : result,
pages : Paginate.links()
}
res.render('items',data);
});
});
});
这是master_data.js中的代码
class Pagination{
constructor(totalCount,currentPage,pageUri,perPage=2){
this.perPage = perPage;
this.totalCount = parseInt(totalCount);
this.currentPage = parseInt(currentPage);
this.previousPage = this.currentPage + 1;
this.nextPage = this.currentPage - 1;
this.pageCount = Math.ceil(this.totalCount / this.perPage);
this.pageUri = pageUri;
this.offset = this.currentPage > 1 ? this.previousPage * this.perPage : 0;
this.sidePage = 4;
this.pages =false;
}
links() {
this.pages='<ul class="pagination">';
if(this.previousPage > 0)
this.pages+='<li class="page-item"><a class="page-link" href="'+this.pageUri + this.previousPage+'">Previous</a></li>';
if(this.currentPage > 1){
for(var x = this.currentPage - this.sidePage; x < this.currentPage; x++){
if(x>0)
this.pages+='<li class="page-item"><a class="page-link" href="'+this.pageUri+x+'">'+x+'</a></li>';
}
}
this.pages+= '<li class="page-item active"><a class="page-link" href="'+this.pageUri+this.currentPage+'">'+this.currentPage+'</a></li>';
for(x = this.nextPage; x <= this.pageCount; x++){
this.pages+='<li class="page-item"><a class="page-link" href="'+this.pageUri+x+'">'+x+' </a></li>';
if(x >= this.currentPage + this.sidePages)
break;
}
if(this.currentPage + 1 <= this.pageCount)
this.pages+='<li class="page-item"><a class="page-link" href="'+this.pageUri+this.nextPage+'">Next</a></li>';
this.pages+='</ul>';
return this.pages;
}
}
module.exports = Pagination;
但是我得到这样的错误。
TypeError: Class constructor Pagination cannot be invoked without 'new'
at Layer.handle [as handle_request] (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/layer.js:95:5)
at /Users/Documents/code/prediksi-app/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/index.js:335:12)
at next (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/Users/Documents/code/prediksi-app/node_modules/body-parser/lib/types/json.js:110:7)
at Layer.handle [as handle_request] (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/Documents/code/prediksi-app/node_modules/express/lib/router/index.js:317:13)
答案 0 :(得分:0)
尝试一下:
在master_data.js中:将module.exports
更改为
module.exports = {
Pagination
};
And in your index.js import(require) it like this
let pagination = require('./master_data');
let Pagination = pagination.Pagination;