添加产品类别数

时间:2019-06-06 03:09:06

标签: node.js mongodb express mongoose

我想在类别中添加字段,其中包含与此类别相关的产品数量 我的代码是

exports.paginate = async (Model,page,limit,sort,populate) => {
     const Product = require('../models/product')
     const count = await Model.countDocuments()
     const pages = Math.ceil(count/limit)
     const skip = (page - 1) * limit
     let data = await Model
     .find()
     .populate(populate)

     .limit(parseInt(limit))
     .skip(parseInt(skip))
     .sort(sort)
     let data2 = data.map(  (item) => {
          item.set('productCount', Product.find().countDocuments(),{strict:false});
          return item

     })
     return {
          data2,
          pages,
          totalItems:count,
          lastPage:pages,
          nextPage:parseInt(page)+1
     }
}

1 个答案:

答案 0 :(得分:0)

经过多次尝试,我找到了解决方案

 import { Component, OnInit } from '@angular/core';
    import { Location } from '@angular/common';
    import {Router, ActivatedRoute, NavigationEnd, RoutesRecognized, Params, PRIMARY_OUTLET} from '@angular/router'
    constructor(location: Location, activate: ActivatedRoute, router:Router) {

      router.events.subscribe((val) => {
      if (location.path() !== '') {
        this.route = location.path();
        this.breadcrumbListArray = this.route.split('/');
        for(let d of this.breadcrumbListArray) {
         this.breadcrumbListObj["link"] = "/" + d;
         this.breadcrumbListObj["name"] = d;
        }
      }

  });