NodeJS 无法将中的用户服务导入报表控制器

zmeyuzjn  于 2023-03-01  发布在  Node.js
关注(0)|答案(1)|浏览(106)

大家好,我正在做一个嵌套的Js迷你项目,我有两个独立的模块,分别叫做报表和用户,它们都有自己的服务,控制器和存储库。
报表与用户具有多对一的关系。
我想在报告中添加用户属性。
我尝试在报表控制器中导入用户的服务类。请查找以下代码:
users.service.ts

import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Users } from './users.entity';

@Injectable()
export class UsersService {
    constructor(
        @InjectRepository(Users) private userRepository: Repository<Users>
    ) {
        userRepository.remove = userRepository.softRemove;
        userRepository.delete = userRepository.softDelete;
    }
    createUser(body: object) {
        const instance = this.userRepository.create(body);
        return this.userRepository.save(instance);
    }

    find(query: object) {
        return this.userRepository.find({ "where": query })
    }

    findOne(id: any) {
        if (!id) {
            throw new BadRequestException('Id not found')
        }
        return this.userRepository.findOne({ "where": { "id": id } });
    }

    async update(id: string, record: Partial<Users>) {
        var user = await this.findOne(id);
        if (!user) {
            return new NotFoundException('User Not Found');
        }
        return this.userRepository.save(Object.assign(user, record));
    }

    async delete(id: string) {
        var user = await this.findOne(id);
        if (!user || user === null) {
            throw new NotFoundException('User Not Found');
        }
        else {
            this.userRepository.remove(user)
            return "Successfully deleted"
        }
    }
}

reports.controller.ts

import { Body, ClassSerializerInterceptor, Controller, Post, Session, UseGuards, UseInterceptors } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';
import { ReportsService } from './reports.service';

@Controller('reports')
export class ReportsController {
    constructor(
        private reportsService: ReportsService,
        private usersService:UsersService
    ) { }
}

但我得到了一些错误。请看截图也Error Screenshot
我被困在这里很长一段时间,请帮助我解决这个问题。请在我的github上找到更多的代码文件:https://github.com/Rakshith083/NestJs-Car-Pricing/tree/master/src

eh57zj3b

eh57zj3b1#

当使用模块和共享服务时,服务的发起模块(它首先注册的地方)应该在providersexports数组中都有该服务,以便将该服务注册到DI容器并使其对其他模块可用,然后,当该服务需要在不同模块的服务中使用时,服务的起始模块应添加到新服务的起始模块的imports数组中。在您的示例中,UsersModule应将UsersService添加到exports数组中,ReportsModule应将UsersModule添加到imports数组中。

import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersService } from './users.service';
import { Users } from './users.entity';
import { AuthService } from 'src/auth/auth.service';

@Module({
  imports:[TypeOrmModule.forFeature([Users])],
  controllers: [UsersController],
  providers: [UsersService,AuthService],
  exports: [UsersService],
})
export class UsersModule {}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersModule } from '../users/users.module';
import { ReportsController } from './reports.controller';
import { Reports } from './reports.entity';
import { ReportsService } from './reports.service';

@Module({
  imports:[TypeOrmModule.forFeature([Reports]), UsersModule],
  controllers: [ReportsController],
  providers: [ReportsService]
})
export class ReportsModule {}

您可能还应该考虑将AuthService修复为来自AuthModule,而不是重新创建AuthService,但这只是一些友好的建议

相关问题