【问题标题】:TypeError: Class constructor Pagination cannot be invoked without 'new'TypeError:没有'new'就不能调用类构造函数分页
【发布时间】:2020-01-04 22:35:46
【问题描述】:

我是 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)

【问题讨论】:

  • 我正在尝试,但 ReferenceError: totalCount_value is not defined

标签: javascript node.js pagination


【解决方案1】:

试试这个:

在 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;

【讨论】:

  • 同样的错误。 TypeError:没有'new'就不能调用类构造函数分页
  • 很抱歉,我无法复制您的错误。如果我这样做 let pagination = require('./master_data'); let Pagination = pagination.Pagination let test = new Pagination(10, 5, 'test', 2); console.log(test.links()) 一切正常
猜你喜欢
  • 2018-11-12
  • 2019-04-21
  • 2021-05-17
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 2020-11-27
相关资源
最近更新 更多