“猫鼬-聚集-分页”,不起作用或我缺少某些东西

时间:2019-01-18 03:55:02

标签: node-modules

//下面是我的方法,它将返回汇总查询:

aggregatedQuery = (filters) => {
const {
    name
} = filters;

const aggregateStages = [
  {
    $match: {
        $or: [
            { company: { $regex: name, $options: 'i' } },
        ],
    },
  }
]
return aggregateStages;

};

//下面是我的分页方法,该方法将返回分页的公司,即时通讯使用异步等待将数据发送回我的前端。

  exports.getPaginatedCompany = async (req,res) => {
  const { page, quantity, filters } = req.body;
  const aggregatedPaginatedCompany = await companyName.aggregatePaginate(
    aggregatedQuery(filters), {
      page: +page,
      limit: +quantity,
    },
  )
    .catch(err => res.status(httpStatusCode.INTERNAL_SERVER_ERROR).send({
      message: err.message,
      stackTrace: err.stack,
    }));

  res.status(httpStatusCode.OK).json(aggregatedPaginatedCompany);
}

//下面是我的架构,很确定我已经遵循了文档中的内容,或者我是吗?

const mongoose = require('mongoose');
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate');

const { Schema } = mongoose;

const CompanySchema = Schema(
  {
    company: {
      type: String,
      trim: true,
    },
    contactNum: {
      type: [String],
      require: true,
    },
    email: {
      type: [String],
      require: true,
    },
  },
  { timestamps: { createdAt: 'createdAt' } },
);

CompanySchema.plugin(mongooseAggregatePaginate);

module.exports = mongoose.model('companyName', CompanySchema);

我整夜都在努力使此代码运行,我不明白自己所缺少的内容。

0 个答案:

没有答案