Nest 无法导出提供程序/模块

问题描述 投票:0回答:1

我想通过 NestJS 在我的应用程序中实现与 Google 的连接。 我有以下错误,我不知道如何解决。

[Nest] 7592 - 30/04/2024 00:52:21 错误 [ExceptionHandler] Nest 无法导出不属于当前处理的模块 (GoogleModule) 一部分的提供程序/模块。请验证导出的 GoogleService 在此特定上下文中是否可用。 可能的解决方案: GoogleService 是 GoogleModule 中相关提供者/导入的一部分吗?

我通过修改 google.module.ts 尝试了很多东西,但它不起作用 这是我的代码:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GoogleService } from './google.service';
import { GoogleStrategy } from './google.strategy';
import { GoogleController } from './google.controller';
import { User } from 'src/user/entities/user.entity';
import { SessionSerializer } from '../Serializer';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [GoogleController],
  providers: [
    GoogleStrategy,
    SessionSerializer,
    {
      provide: 'GOOGLE_SERVICE',
      useClass: GoogleService,
    },
  ],
  exports: [GoogleService],
})
export class GoogleModule {}

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/user/entities/user.entity';
import { UserDetails } from 'src/user/types';
import { Repository } from 'typeorm';

@Injectable()
export class GoogleService {
  constructor(
    @InjectRepository(User) private readonly userRepository: Repository<User>,
  ) {}

  async validateUser(details: UserDetails) {
    console.log('GoogleService');
    console.log(details);
    const user = await this.userRepository
      .createQueryBuilder('user')
      .where('user.email = :email', { email: details.email })
      .getOne();

    if (user) {
      console.log(user);
      return user;
    } else {
      console.log('User not found. Creating...');
      const newUser = this.userRepository.create(details);
      return this.userRepository.save(newUser);
    }
  }

  async findUser(id: number) {
    const user = await this.userRepository.findOne({ where: { id } });
    return user;
  }
}

和app.module.ts:

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT),
      synchronize: true,
      autoLoadEntities: true,
      entities: [User, Program, Statistic, Exercise],
    }),
    ConfigModule.forRoot(),
    UserModule,
    GoogleModule,
    PassportModule.register({ session: true }),
  ],
  controllers: [AppController, GoogleController],
  providers: [AppService, GoogleService, GoogleStrategy, FacebookStrategy],
})
export class AppModule {}

提前谢谢您

nestjs google-oauth
1个回答
0
投票

您没有将提供商令牌

GoogleService
添加到
GoodleModule
,您添加了指向类
'GOOGLE_SERVICE'
的提供商令牌
GoogleService
。这意味着您需要导出令牌
'GOOGLE_SERVICE'
删除该自定义提供程序并直接将
GoogleService
添加到
providers
数组

© www.soinside.com 2019 - 2024. All rights reserved.