【发布时间】:2018-08-31 15:51:03
【问题描述】:
mongoose-paginate 不会渲染 mongoose 模型的虚拟字段。有什么解决办法吗?
【问题讨论】:
mongoose-paginate 不会渲染 mongoose 模型的虚拟字段。有什么解决办法吗?
【问题讨论】:
如果您在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.
}
【讨论】:
mongoose-paginate 可以渲染虚拟,但前提是你排除了lea() 操作。
const options = {
// lean: true - exclude this string if you have
sort: '-updatedAt',
// etc.
}
【讨论】: