简体   繁体   English

依赖注入巴别塔巢,特别是猫鼬

[英]Dependency injection of nest in babel particularly with mongoose

I am following the guide of NestJs about Mongoose but with Babel and I'm facing an error trying to inject my schema. 我正在遵循有关Mongoose的NestJs指南,但是使用Babel时,我在尝试注入我的架构时遇到错误。 The code is pretty simple and it is just the same as the typescript examples but with babel. 代码非常简单,与打字稿示例相同,但带有babel。

This is my main module 这是我的主要模块

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { EventModule } from './events/event-module';

@Module({
  imports: [
    MongooseModule.forRoot(process.env.MONGO_URI),
    EventModule,
  ],
})
export class AppModule {}

This is the simplest schema example 这是最简单的架构示例

import { Schema } from 'mongoose';

export const EventSchema = new Schema({
  title: String,
  description: Number,
});

The module event module: 模块事件模块:

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { EventController } from "./event-controller";
import { EventService } from "./event-service";
import { EventSchema } from '../schemas/event-schema';

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: 'Event', schema: EventSchema }
    ])
  ],
  controllers: [EventController],
  providers: [
    EventService,
  ],
})
export class EventModule {}

And finally, the service where I want the schema to be used: 最后,我要使用架构的服务:

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { EventSchema } from '../schemas/event-schema';

@Injectable()
export class EventService {
  @InjectModel(EventSchema) eventModel;
  async getAll() {
    return await this.eventModel.find().exec();
  }
}

The problem is that when I call getAll from the controller, it turns out that eventModel is undefined! 问题是,当我从控制器调用getAll时,事实证明eventModel是未定义的! No error is thrown at instantiation. 实例化时不会引发任何错误。 The error thrown is: 引发的错误是:

TypeError: Cannot read property 'find' of undefined
    at EventService._callee$ (D:\.../event-service.js:9:34)
    at tryCatch (D:\...\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:65:40)

Is it possible that in babel, the dependency injection works different for nestjs ? 难道在babel中,依赖注入对nestjs吗? How the InjectModel is supposed to work or how should it be used with babel? InjectModel应该如何工作或如何与babel一起使用?

Thank you very much in advance, any advice or tip is well appreciated and received 预先非常感谢您,任何建议或提示都将不胜感激并得到好评

Regards 问候

There has a error: 有一个错误:

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { EventSchema } from '../schemas/event-schema';

@Injectable()
export class EventService {
  @InjectModel(EventSchema) eventModel;
  async getAll() {
    return await this.eventModel.find().exec();
  }
}

This line 这条线

@InjectModel(EventSchema) eventModel;

should be @InjectModel('Event') eventModel 应该是@InjectModel('Event') eventModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM