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

592
Vistas
res.redirect is not a function - express.js

I am new to Express.js and am trying to create a URL shortener.

When the user naviagtes to domain.com/r/ I query the DB for the param and get the actual URL.

I use res.redirect to try to redirect to their stored URL but it fails with 'res.redirect is not a function'.

Please see the snippet below:

router.get('/r/:shortUrl', function(req, res) {

  connectDatabase.then(
    checkDatabase(req.params.shortUrl)
      .then( res => {
        console.log('checkdatdabase res => ' + res); //res = 'https://www.google.com'
        res.redirect(res); //TypeError: res.redirect is not a function
      })
      .catch(e => {
        //executed due to above error
        console.error(e);
        res.redirect(307, '/home');
      })
  )
});

Any advice would be much appreciated as this is a learning project for me.

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

0

You have two variables named res.

One, passed to the callback you pass to get() is a response object which has a redirect method.

The other is the resolved value of checkDatabase and is not a response object.

The latter has shadowed the former so you can't access it any more.

Use unique names for your variables to avoid this problem.


I recommend using a linter. ESLint can catch this kind of problem with the no-shadow rule.

about 4 years ago · Juan Pablo Isaza Denunciar

0

datdabaseRes and res should be different variable name. Otherwise res has the respose object of database connection in the scope you are trying to use it.

   

router.get('/r/:shortUrl', function(req, res) {
  connectDatabase.then(
    checkDatabase(req.params.shortUrl)
      .then( datdabaseRes => {
        console.log('checkdatdabase res => ' + datdabaseRes); //res = 'https://www.google.com'
        res.redirect(datdabaseRes); //TypeError: res.redirect is not a function
      })
      .catch(e => {
        //executed due to above error
        console.error(e);
        res.redirect(307, '/home');
      })
  )
});

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