创建NestFactory后如何将Mongoose插件设置为架构

问题描述 投票:0回答:1

[我正在使用nest,在设置猫鼬插件之前,我需要调用另一个服务来获取密钥,我试图在main.ts中初始化mongoose插件,但是它不起作用,下面是我的工作

  • 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();
typescript mongoose nestjs mongoose-plugins
1个回答
0
投票

您收到错误消息还是只是未设置?您是否尝试过从注入架构的服务构造函数中调用它?但是我使用以下内容在mongodb中定义架构]

import { Schema } from 'mongoose';

export const TestSchema = new Schema(
    {
      test: String;
    }
);
© www.soinside.com 2019 - 2024. All rights reserved.