Integeek
Integeek

Reputation: 1

Nest cannot export a provider / module

I want to implement the connexion with Google in my application thanks to nestJS. I have the following error and I don't know how to solve it.

[Nest] 7592 - 30/04/2024 00:52:21 ERROR [ExceptionHandler] Nest cannot export a provider/module that is not a part of the currently processed module (GoogleModule). Please verify whether the exported GoogleService is available in this particular context. Possible Solutions: Is GoogleService part of the relevant providers/imports within GoogleModule?

I tried many things by modifying the google.module.ts but it doesn't work Here is my code :

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;
  }
}

and the 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 {}

Thank you in advance

Upvotes: 0

Views: 272

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70490

You didn't add GoogleService, the provider token, to the GoodleModule, you added the provider token 'GOOGLE_SERVICE' which points to the class GoogleService. This means that you need to export the token 'GOOGLE_SERVICE' or remove that custom provider and just add GoogleService to the providers array directly

Upvotes: 1

Related Questions