我正在使用nest,我需要在设置猫鼬插件之前调用另一个服务来获取密钥,我尝试在其中初始化猫鼬插件main.ts
,但它不起作用,下面是我所做的
- 测试模式.ts
import { Document } from 'mongoose';
export class TestSchema extends Document{
readonly test: String;
}
- 初始化架构.ts
export const initMongoosePlugin = () => {
TestSchema.plugin(plugin, {
fields: ['test'],
secret: global['KEY'], // init it in main.ts
});
};
- main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const appService: AppService = app.get(AppService);
global['KEY'] = await appService.getKey();
// init Mongoose Plugin
initMongoosePlugin();
app.use(requestIp.mw());
await app.listen(3000));
}
bootstrap();