【发布时间】:2020-07-13 15:30:58
【问题描述】:
我正在使用 NodeJS、MongoDB 和 Mongoose。如果我使用findById(this.id),但不能直接使用this,我可以填充引用的文档:
IssuanceSchema.methods.getOrganizationName = async function() {
let issuance = await Issuance.findById(this.id).populate('organization');
console.log(issuance);
let temp = this.populate('organization');
console.log(temp);
console.log(issuance.organization.displayName());
console.log(temp.organization.displayName());
// ...
}
日志显示:
{ _id: 5e849ca9b07ed81bd2eaad89, 组织: { _id: 5e80a19d8c910f196c11673c, ... }, } { _id: 5e849ca9b07ed81bd2eaad89, 组织:5e80a19d8c910f196c11673c, } 某个名字 (node:10231) UnhandledPromiseRejectionWarning: TypeError: temp.organization.displayName is not a function我怎样才能直接填充引用的文档而无需迂回findById?
【问题讨论】: