猫鼬execPopulate在文档上不使用新填充的记录更新虚拟属性

时间:2018-11-30 01:41:29

标签: mongoose mongoose-schema mongoose-populate

我有一个带有虚拟属性的模型,该模型指向另一个集合。通过模型类查询该模型并填充相关文档时,虚拟属性正确包含相关文档。但是,从文档本身填充不会更新虚拟属性。当_doc属性包含填充的文档时,虚拟属性为null。

class ChimeraModel extends mongoose.Model {
    /**
     * @async
     * Compiles and registers the defined ChimeraModel into a mongoose Model by:
     * 1.) Populating the ChimeraModel with all related fields, validators, associations, etc.
     * 2.) Unregistering this model from mongoose if it has already been registered
     * 3.) Registering a new mongoose model from details of the ChimeraModel
     * @returns {Promise<mongoose.Model>} - The newly registered mongoose Model
     */
    async compile () {
        mongoose.set('debug', true);
        const thing = await this.constructor.findById(this.id).populate('chimeraFields').exec();
        const thing2 = await this.populate('chimeraFields').execPopulate();

        console.log(thing.chimeraFields); // Array(3) [model, model, model]
        console.log(thing2.chimeraFields); // null
        console.log(thing2._doc.chimeraFiels); // Array(3) [model, model, model]

    }
}

0 个答案:

没有答案
相关问题