I'm using nest, I need to call another service to get a key before setting a mongoose plugin, I tried to init the mongoose plugin in main.ts
, but it doesn't work, below is what I did
- test.schema.ts
import { Document } from 'mongoose';
export class TestSchema extends Document{
readonly test: String;
}
- init.schema.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();