Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
TypeError: no se puede leer la propiedad 'authService' de undefined con TypeDi

Creo una clase 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

Servicio de autenticación:

 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;

Cuando ejecuto el servidor, me sale este error:

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

Declaré el contenedor para el enrutador de autenticación, pero creo que el AuthController no está inyectado. ¿Cómo puedo arreglar eso?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Actualicé el método getRouter para solucionar el problema:

 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda