Nest.js依赖注入报错:BookingModule无法解析HotelsRepository
NestJS依赖注入报错:无法解析HotelsRepository解决方案
报错信息
[Nest] 2348 - 04/11/2023, 23:08:12 ERROR [ExceptionHandler] Nest can't resolve dependencies of the BookingOrderService (BookingOrdersRepository, BookingOrderDetailRepository, ?). Please make sure that the argument HotelsRepository at index [2] is available in the BookingModule context.
问题原因
你在BookingOrderService的构造函数中注入了HotelsRepository,但该Repository并未在BookingModule中注册——TypeOrmModule.forFeature数组里没有包含Hotels实体,Nest无法在当前模块上下文找到对应的依赖。
解决方案
修改module.module.ts,在TypeOrmModule.forFeature中添加Hotels实体:
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { BookingOrderService } from '../booking/booking_order/booking_order.service'; import { BookingOrderDetailService } from '../booking/booking_order_detail/booking_order_detail.service'; import { BookingOrderDetailExtraService } from '../booking/booking_order_detail_extra/booking_order_detail_extra.service'; import { UserBreakfeastService } from '../booking/user_breakfeast/user_breakfeast.service'; import { SpecialOfferCouponsService } from '../booking/special_offer_coupons/special_offer_coupons.service'; import { SpecialOfferService } from '../booking/special_offer/special_offer.service'; import { SpecialOfferController } from '../booking/special_offer/special_offer.controller'; import { SpecialOfferCouponsController } from '../booking/special_offer_coupons/special_offer_coupons.controller'; import { UserBreakfeastController } from '../booking/user_breakfeast/user_breakfeast.controller'; import { BookingOrderDetailExtraController } from '../booking/booking_order_detail_extra/booking_order_detail_extra.controller'; import { BookingOrderDetailController } from '../booking/booking_order_detail/booking_order_detail.controller'; import { BookingOrderController } from '../booking/booking_order/booking_order.controller'; import { BookingOrderDetail } from 'output/entities/BookingOrderDetail'; import { BookingOrderDetailExtra } from 'output/entities/BookingOrderDetailExtra'; import { BookingOrders } from 'output/entities/BookingOrders'; import { UserBreakfeast } from 'output/entities/UserBreakfeast'; import { SpecialOfferCoupons } from 'output/entities/SpecialOfferCoupons'; import { SpecialOffers } from 'output/entities/SpecialOffers'; // 导入Hotels实体 import { Hotels } from 'output/entities/Hotels'; @Module({ imports: [TypeOrmModule.forFeature([ //FOR BOOKING BookingOrderDetail, BookingOrderDetailExtra, BookingOrders, UserBreakfeast, SpecialOfferCoupons, SpecialOffers, // 添加Hotels实体到Repository注册列表 Hotels, ]), ], providers: [BookingOrderService, BookingOrderDetailService, BookingOrderDetailExtraService, UserBreakfeastService, SpecialOfferCouponsService, SpecialOfferService], controllers: [SpecialOfferController, SpecialOfferCouponsController, UserBreakfeastController, BookingOrderDetailExtraController, BookingOrderDetailController, BookingOrderController], exports: [], }) export class BookingModule { }
额外优化建议
- 实体关联优化:当前
CreateBooking方法直接接收前端传来的Hotels实体对象,存在数据安全风险。建议改为接收hotelId,在服务中通过HotelsRepository查询对应的酒店实体后再关联保存:
// 修改CreateBooking方法参数 public async CreateBooking( // ...其他参数 hotelId: number, ) { try { const boorHotel = await this.hotelService.findOne({ where: { hotelId } }); if (!boorHotel) { return '酒店不存在'; } await this.boorService.save({ // ...其他字段 boorHotel: boorHotel, }) } catch (error) { return error.message; } }
同时对应修改booking_order.controller.ts的Create方法,将@Body('boorHotel') boorHotel: Hotels改为@Body('hotelId') hotelId: number。
- 模块拆分(可选):如果酒店相关业务有独立的模块(比如
HotelModule),可以将Hotels实体的Repository注册到HotelModule并导出,然后在BookingModule的imports中导入HotelModule,这样更符合Nest的模块化设计。
内容的提问来源于stack exchange,提问作者kucingcat
相关产品推荐
相关产品推荐

