非同期プラグインをアタッチする際に、スキーマにメソッドを追加することはできますか?
const anotherModel = require(__dirname + "/anotherModel.js")
const mongoose = require("mongoose")
let injectPlugin = async (schema, options) => {
const anotherObject = await anotherModel.findOne({name: options['anotherModelName'] }).lean();
schema.methods.testfunction = async () => {
console.log(anotherObject)
}
}
module.exports = injectPlugin
上記のサンプル スニペットでは、別のモデルを初期化してロードし、そのデータをプラグイン内の後続の操作に使用しようとしています。
非同期メソッドなので、promise が実行される前にプラグインが読み込まれ、エラーが発生します
TypeError: sampleEntity.testfunction is not a function
では、非同期プラグイン内のスキーマに非同期メソッドをアタッチすることは可能ですか?