[英]Can't resolve dependencies of the OrderService. Mongoose, NestJS
I'm completely new to NestJS and I struggle with a little problem.我对 NestJS 完全陌生,我遇到了一个小问题。 While creating an order service to my shop, I want to get access to all my books from my database - I'm using mongoose to manage database.在为我的商店创建订单服务时,我想从我的数据库中访问我的所有书籍 - 我正在使用 mongoose 来管理数据库。 But while creating bookModel in my orderService, in order to get access to them, Nest return me that error.但是在我的 orderService 中创建 bookModel 时,为了访问它们,Nest 向我返回了该错误。
Error: Nest can't resolve dependencies of the OrderService (OrderModel, ?). Please make sure that the argument BookModel at index [1] is available in the OrderModule context.
Potential solutions:
- If BookModel is a provider, is it part of the current OrderModule?
- If BookModel is exported from a separate @Module, is that module imported within OrderModule?
@Module({
imports: [ /* the Module containing BookModel */ ]
})
Here is my order.service.ts:这是我的 order.service.ts:
@Injectable()
export class OrderService {
constructor(@InjectModel('Order') private orderModel:Model<Document & Order>,@InjectModel('Book') public bookModel:Model<Document & Book>)
{
}
async getOrders() {
///some code
}
async addOrder(@Body() createOrderDto: CreateOrderDto){
///some code
}
}
My order.module.ts:我的 order.module.ts:
@Module({
imports: [MongooseModule.forFeature([{name:'Order',schema: OrderSchema }])],
providers: [OrderService],
controllers: [OrderController]
})
export class OrderModule {}
And my book.module.ts:还有我的 book.module.ts:
@Module({
imports: [MongooseModule.forFeature([{name:'Book',schema: BookSchema }])],
controllers: [BookController],
providers: [BookService],
exports:[BookService]
})
export class BookModule {}
The error states it pretty clearly.该错误非常清楚地说明了这一点。 The OrderModule
doesn't have access to the BookModel
( MongooseModule.forFeature([{name: 'Book', schema: BookSchema}])
).You can export the MongooseModule
from BookModule
and add BookModule
to the imports
array of OrderModule
to quickly resolve this. OrderModule
无权访问BookModel
( MongooseModule.forFeature([{name: 'Book', schema: BookSchema}])
)。您可以从BookModule
导出MongooseModule
并将OrderModule
添加到BookModule
的imports
数组中以快速解决这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.