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

问题描述

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

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)