【问题标题】:mongoose-paginate does not render virtual fields of the mongoose modelmongoose-paginate 不渲染 mongoose 模型的虚拟字段
【发布时间】:2018-08-31 15:51:03
【问题描述】:

mongoose-paginate 不会渲染 mongoose 模型的虚拟字段。有什么解决办法吗?

https://github.com/edwardhotchkiss/mongoose-paginate

【问题讨论】:

    标签: arrays mongoose


    【解决方案1】:

    如果您在schema level 使用虚拟配置,那么此设置可能会为您呈现虚拟配置

    const options = {
        // lean: true - exclude this string if you have
        sort: '-updatedAt',
        // etc.
    }
    

    或者如果你像我一样在文档级别添加虚拟配置,例如:

    // jobModel is my job schema and questions is a virtual collection mapped from questions schema
    const doc = await this.jobModel.findOne({ _id: id }).populate({ path: 'questions' });
    
    // This returns me the parent doc and the virtual data
    return doc ? doc.toObject({ virtuals: true }) : null; 
    

    那么您可能需要保留lean: true 以便像这样填充虚拟字段:

    const options = {
        lean: true,
        sort: '-updatedAt',
        // etc.
    }
    

    【讨论】:

      【解决方案2】:

      mongoose-paginate 可以渲染虚拟,但前提是你排除了lea() 操作。

       const options = {
              // lean: true - exclude this string if you have
              sort: '-updatedAt',
              // etc.
        }
      

      【讨论】:

        猜你喜欢
        • 2015-06-09
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 2016-02-05
        • 2020-05-16
        • 2012-11-07
        • 2018-09-16
        相关资源
        最近更新 更多