I am following the guide of NestJs about Mongoose but with Babel and I'm facing an error trying to inject my schema. The code is pretty simple and it is just the same as the typescript examples but with 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! 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
? How the InjectModel
is supposed to work or how should it be used with 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.