Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

585
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!