0

在附加异步插件时,是否可以向架构添加方法?

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

那么是否可以将异步方法附加到异步插件中的模式?

4

0 回答 0