1

我遵循了nestjs网站上的MongoDB教程,但无法在我自己的项目中复制它,我已经克隆了nextjs repo并验证了那里提供的mongo示例运行良好。

这是我目前拥有的(缩小):

app.module.ts

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/nest')
  ],
  modules: [
    CampaignModule
  ]
})
export class AppModule implements NestModule {}
}

活动模块.ts

@Module(
  {
    modules: [SharedModule],
    controllers: [CampaignController],
    components: [CampaignService],
    imports: [MongooseModule.forFeature([{name: 'Campaign', schema: campaignSchema}])]
  })
export class CampaignModule {}

活动服务.ts

@Component()
export class CampaignService {
  constructor(@InjectModel(campaignSchema) private readonly campaignModel: Model<CampaignInterface>) {}
}

这可以按预期构建,但是当我运行它时出现此错误:

Nest can't resolve dependencies of the CampaignService (?). Please verify whether [0] argument is available in the current context.

因此,因为在我的 app.module.ts 中,我的模块部分中有活动模块(到目前为止,我如何让它在 mongodb 之前运行良好),所以我将它移动到带有 MongooseModule 的导入部分,现在它可以正常构建和运行,但是campaign.controller.ts 控制器内部的路由根本没有注册,它们只是返回404。这很奇怪,因为nestjs repo 中的示例在带有MongooseModule 的导入中包含CatsModule 并且它可以工作。

这里的任何提示将不胜感激。这也是我的活动对象的架构和界面:

export interface CampaignInterface extends Document, Campaign {
}

export const campaignSchema: any = new mongoose.Schema({
  campaignId: String
});

编辑 1 我将所有模块移动到我的 app.module.ts 文件的导入部分,就像我在导入中使用 CampaignModule 和模块中的所有其他模块一样尝试它:[]。现在,当出现相同的错误并且没有注册任何路由时,它会立即失败。

4

1 回答 1

0

好的,我知道发生了什么,

似乎如果您在 @Module 装饰器中同时有一个模块:[] 和导入:[],则模块:[] 接管并完全忽略导入:[]。因此,在我的campaign.module.ts 文件中,我在MongooseModules.forFeature 导入之后将SharedModule 从modules: [] 移到imports: [] 中。它现在可以工作并写入 mongo!

于 2018-02-06T19:27:09.047 回答