0

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();
troy
  • 2,145
  • 2
  • 23
  • 31

1 Answers1

-1

Do you get error or it is just not set? Have you tried to call it from the service constructor in which you inject the schema? However I use the following to define a schema in mongodb

import { Schema } from 'mongoose';

export const TestSchema = new Schema(
    {
      test: String;
    }
);
tano
  • 2,657
  • 1
  • 15
  • 16