在mongoose中链接填充导致错误

kgqe7b3p  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(101)

我在Mongoose中链接填充有问题。
我有一个pet schema,我声明了一个这样的方法:

petSchema.methods.populateForLang = async function(lang) {
...
}

字符串
在这个方法中,我试图填充一些文档,这个填充工作得很好:

return await this.populate({path:'type', select: 'title en'});


我也有这个工作正常的填充:

return await this.populate({path: 'petGender', select: 'title en'});


但当我把它们连在一起时:

await this.populate({path: 'petGender', select: 'title en'}).populate({path:'type', select: 'title en'});


我得到的错误:

this.populate(...).populate is not a function


怎么回事?

ctehm74n

ctehm74n1#

如果您需要填充多个模式,则可以将它们作为数组模型填充到一个模式中:

await this.populate([{ path: 'petGender', select: 'title en' }, { path:'type', select: 'title en' }]);

字符串

相关问题