我一直在mongoose-paginate方法上得到TypeError;

时间:2015-12-24 07:15:13

标签: javascript node.js mongodb mongoose pagination

我一直在:

  

TypeError undefined paginate function

这是代码

var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var paginate = require('mongoose-paginate');
var Schema = mongoose.Schema;

var ProductSchema = new Schema({
  name: String,
});

ProductSchema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]});

ProductSchema.plugin(paginate);

module.exports = mongoose.model('Product', ProductSchema);

在router.js

app.get('/', function(req, res) {
   Product.paginate({}, { page: 3, limit: 10 }, function(err, result) {

   });
});

我在router.js中收到此错误,它一直说:

  

TypeError undefined paginate function

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我用Google搜索无效(下面列出的其他类似问题)

我console.logged mongoose-paginate,结果证明它是一个接受模式的函数。我在我的架构上调用它,它现在对我有效。

猫鼬-PAGINATE:^ 5.0.0

// Load required packages
var mongoose = require('mongoose');
var timestamps = require('mongoose-timestamp');
var mongoosePaginate = require('mongoose-paginate');

// Define our Question schema
var QuestionSchema = new mongoose.Schema({
    text: {type: String, unique: true, required: true},
    type: {type: String, required: true},
    options: [String]
});

//Add auto timestamping
QuestionSchema.plugin(timestamps);

//Add Pagination
mongoosePaginate(QuestionSchema);

// Export the Mongoose model
module.exports = mongoose.model('questions', QuestionSchema);

节点:v4.4.3

npm:2.15.1

猫鼬:^ 4.4.12

如果这没有帮助,您可以尝试这些来源:

mongoose-paginate Issue#9

mongoose-paginate Issue#71