Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
TypeError: Cannot read property 'authService' of undefined with TypeDi

I create a class AuthRouter:

import { Router } from 'express'
import Container, { Service } from 'typedi'
import AuthenticationController from './index'

@Service()  
class AuthenticationRouter {
  constructor (private readonly authenticationController: AuthenticationController) {}

  getRouter () {
    const router = Router()
    router.get('/auth/url', this.authenticationController.getAuthUrl)

    return router
  }
}

const authRouter = Container.get(AuthenticationRouter)
const routes = authRouter.getRouter()

export default routes

AuthController.ts:

import { Request, Response } from 'express'
import { Service } from 'typedi'
import AuthenticationService from './authentication-service'

@Service()
class AuthenticationController {
  constructor (private readonly authService: AuthenticationService) {}

  async getAuthUrl (req: Request<{}, {}, {}, {redirect: string}>, res: Response) {
    return res.redirect(this.authService.generateAuthenticationUrl(req.query.redirect))
  }

  
}

export default AuthenticationController

AuthenticationService:

import { OAuth2Client } from 'google-auth-library'
import { google, oauth2_v2 } from 'googleapis'
import { Service } from 'typedi'


@Service()
class AuthenticationService {
  private oauth2Client: OAuth2Client;
  private auth: oauth2_v2.Oauth2
  constructor () {
    this.oauth2Client = new google.auth.OAuth2({
      clientId: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      redirectUri: process.env.REDIRECT_URI
    })
    this.auth = google.oauth2({
      version: 'v2',
      auth: this.oauth2Client
    })
  }

  generateAuthenticationUrl (redirect: string) {
    const url = this.oauth2Client.generateAuthUrl({
      scope: ['email', 'profile'],
      access_type: 'offline',
      state: redirect || '/'
    })
    return url
  }

}

export default AuthenticationService;

When I run the server, I got this error:

(node:4470) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'authService' of undefined

I declared the container for the authenticationRouter but I think the AuthController is not injected. How can I fix that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I updatedt the getRouter method to fix the issue:

getRouter () {
    const router = Router()
    router.get('/google/url', (req: Request<{}, {}, {}, {redirect: string}>, res) => this.authenticationController.getAuthUrl(req, res))

    return router
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda