在NestJS中使用Mongoose鉴别器时覆盖删除错误

在NestJS中使用Mongoose鉴别器时覆盖删除错误,mongoose,nestjs,mongoose-schema,discriminator,Mongoose,Nestjs,Mongoose Schema,Discriminator,我接着介绍了如何实现Mongoose鉴别器,但令我惊讶的是,我不断地得到覆盖emodeleror。我花了好几个小时在不同的示例项目中找出问题,但这些努力都没有取得成果 我的项目树与添加一个带有3个模式的事件模块一样简单: - src |- event | |- click-link-event.schema.ts | |- event.module.ts | |- event.schema.ts | |- sign-up-event.sche

我接着介绍了如何实现Mongoose鉴别器,但令我惊讶的是,我不断地得到
覆盖emodeleror
。我花了好几个小时在不同的示例项目中找出问题,但这些努力都没有取得成果

我的项目树与添加一个带有3个模式的事件模块一样简单:

- src
   |- event
   |    |- click-link-event.schema.ts
   |    |- event.module.ts
   |    |- event.schema.ts
   |    |- sign-up-event.schema.ts
   |- app.module.ts
   |- main.ts
下面是每个文件中的代码:

//main.ts
从'@nestjs/core'导入{NestFactory};
从“./app.module”导入{AppModule};
异步函数引导(){
const app=wait NestFactory.create(AppModule);
等待应用程序。聆听(3000);
}
bootstrap();
//app.module.ts
从'@nestjs/common'导入{Module};
从'@nestjs/mongoose'导入{MongooseModule};
从“./event/event.module”导入{EventModule};
@模块({
进口:[
MongooseModule.forRoot('mongodb://localhost:27017/test_db'),
事件模块,
],
})
导出类AppModule{}
//event.module.ts
从'@nestjs/common'导入{Module};
从'@nestjs/mongoose'导入{MongooseModule};
从“./Event.schema”导入{Event,EventSchema};
进口{
单击LinkedEvent,
单击LinkedEventSchema,
}从“./单击链接事件.schema”;
从“./sign-up-event.schema”导入{SignUpEvent,SignUpEventSchema};
@模块({
进口:[
MongooseModule.forFeature([
{
名称:Event.name,
schema:EventSchema,
鉴别器:[
{name:ClickedLinkEvent.name,架构:ClickedLinkEventSchema},
{name:SignUpEvent.name,schema:SignUpEventSchema},
],
},
]),
],
})
导出类EventModule{}
//event.schema.ts
从'@nestjs/mongoose'导入{Schema,SchemaFactory,Prop};
从“”导入{ClickedLinkEvent}/单击链接事件.schema';
从“./sign-up-event.schema”导入{SignUpEvent};
@架构({discriminatorKey:'kind'})
导出类事件{
@支柱({
类型:字符串,
要求:正确,
枚举:[ClickedLinkEvent.name,SignUpEvent.name],
})
种类:弦;
@属性({type:Date,required:true})
时间:日期;
}
export const EventSchema=SchemaFactory.createForClass(事件);
//单击-link-event.schema.ts
从'@nestjs/mongoose'导入{Schema,SchemaFactory,Prop};
@模式()
导出类ClickedLinkEvent{
种类:弦;
时间:日期;
@属性({type:String,必需:true})
url:string;
}
导出常量ClickedLinkedEventSchema=SchemaFactory.createForClass(
单击LinkedEvent,
);
//sign-up-event.schema.ts
从'@nestjs/mongoose'导入{Schema,SchemaFactory,Prop};
@模式()
导出类注册事件{
种类:弦;
时间:日期;
@属性({type:String,必需:true})
用户:字符串;
}
export const SignUpEventSchema=SchemaFactory.createForClass(SignUpEvent);
如您所见,我所做的只是复制文档中使用的代码。这是我在执行npm运行start:dev时遇到的错误:

[Nest]4046-01/11/2021,凌晨1:37:16[NestFactory]正在启动Nest应用程序。。。
[Nest]4046-01/11/2021,凌晨1:37:16[InstanceLoader]应用模块依赖项已初始化
[Nest]4046-01/11/2021,凌晨1:37:16[InstanceLoader]MongosMemodule依赖项已初始化+0ms
[Nest]4046-01/11/2021,凌晨1:37:16[InstanceLoader]事件模块依赖项已初始化+1ms
[Nest]4046-01/11/2021,凌晨1:37:16[InstanceLoader]MongooseCoremoule依赖项已初始化+19ms
[Nest]4046-01/11/2021,凌晨1:37:16[ExceptionHandler]编译后无法覆盖'ClickedLinkEvent'模型+19ms
OverwriteModeError:编译后无法覆盖'ClickedLinkEvent'模型。
在Function.Model.discriminator(/home/saeed/sources/playway/discriminators/event test/node_modules/mongoose/lib/Model.js:1137:11)
at/home/saeed/sources/playdry/discriminators/event test/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:15:56 at Array.forEach()
在addDiscriminators(/home/saeed/sources/playdry/discriminators/event test/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:15:20)
在InstanceWrapper.useFactory[作为元类型](/home/saeed/sources/playdry/discriminators/event test/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:25:17)
在Injector.instantialClass(/home/saeed/sources/playerd/discriminators/event test/node_modules/@nestjs/core/Injector/Injector.js:289:55)
在回调时(/home/saeed/sources/playway/discriminators/event test/node_modules/@nestjs/core/injector/injector.js:42:41)
在处理和拒绝时(内部/process/task_queues.js:93:5)
在async Injector.resolveConstructorParams(/home/saeed/sources/player/discriminators/event test/node_modules/@nestjs/core/injec-tor/Injector.js:114:24)
在async Injector.loadInstance(/home/saeed/sources/playerd/discriminators/event test/node_modules/@nestjs/core/Injector/Injector.js:46:9)上
在async Injector.loadProvider(/home/saeed/sources/playerd/discriminators/event test/node_modules/@nestjs/core/Injector/Injector.js:68:9)
async InstanceLoader.createInstancesOfProviders(/home/saeed/sources/played/discriminators/event test/node_modules/@nestjs/co-re/injector/instance-loader.js:43:9)的async Promise.all(索引5)
在async/home/saeed/sources/playway/discriminators/event test/node_modules/@nestjs/core/injector/instance loader.js:28:13 at async Promise.all(索引5)
在async InstanceLoader.createInstances(/home/saeed/sources/playway/discriminators/event test/node_modules/@nestjs/core/injector/instance loader.js:27:9)

这是NestJS处理它的方式中的一个错误还是我遗漏了这么愚蠢的东西?

我用Mongoose.forFeatureAsync替换Mongoose.forFeature解决了这个问题,希望这能有所帮助:

MongooseModule.forFeatureAsync([
  {
    name: Event.name,
    useFactory: () => {
      return EventSchema;
    },
    discriminators: [
      { name: ClickedLinkEvent.name, schema: ClickedLinkEventSchema },
      { name: SignUpEvent.name, schema: SignUpEventSchema },
    ],
  },
]),
看看这条线。可能为ClickedLinkEvent.name和SignUpEvent.name显式设置字符串会有所帮助。当我尝试访问模式的name属性时,我有一个智能感知器