Sails: res.redirect not redirecting

Created on 7 Jan 2020  路  8Comments  路  Source: balderdashy/sails

Node version: 10.16.3
Sails version _(sails)_: 1.2.3


I try to execute res.redirect ('/'), but console.log gives meundefined and also I get the error

error: Sending 500 ("Server Error") response:
聽 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
聽聽聽聽 at ServerResponse.setHeader (_http_outgoing.js: 470: 11)

my code:

module.exports = {
testq: function (req, res) {
      console.log('redirect', res.redirect('/'));
      return res.redirect('/')
   }
}

``

// config\routes.js
module.exports.routes = {
'/': {view: 'pages/homepage'},
}

Please tell me why res.redirect not redirecting and in what cases this error may occur?

helpful info or workaround question

All 8 comments

@alex-jss Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

  • look for a workaround. _(Even if it's just temporary, sharing your solution can save someone else a lot of time and effort.)_
  • tell us why this issue is important to you and your team. What are you trying to accomplish? _(Submissions with a little bit of human context tend to be easier to understand and faster to resolve.)_
  • make sure you've provided clear instructions on how to reproduce the bug from a clean install.
  • double-check that you've provided all of the requested version and dependency information. _(Some of this info might seem irrelevant at first, like which database adapter you're using, but we ask that you include it anyway. Oftentimes an issue is caused by a confluence of unexpected factors, and it can save everybody a ton of time to know all the details up front.)_
  • read the code of conduct.
  • if appropriate, ask your business to sponsor your issue. _(Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)_
  • let us know if you are using a 3rd party plugin; whether that's a database adapter, a non-standard view engine, or any other dependency maintained by someone other than our core team. _(Besides the name of the 3rd party package, it helps to include the exact version you're using. If you're unsure, check out this list of all the core packages we maintain.)_

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

Hi, @alex-jss! I'd like a bit more context to better understand your issue. Would you mind providing a minimal repo that reproduces this error? Thanks!

repo unfortunately there is no way to provide. I have provided all possible information.
The challenge is to redirect to any given page

I changed my redirect request to another page. In the developer panel - network, I get preview of page I want, but the error remains and the redirect is not performed.

my code:

module.exports = {
  testq: function (req, res) {
     console.log('redirect', res.redirect('/views/index/index.html'));
     res.redirect('/views/index/index.html')
  }
}

I attach a mistake
err_redirect.txt

I think you should check the Notes section of the docs for a possible help. You seem to be doing everything nicely but just to make sure. Check the docs

Hey, @alex-jss!

I've got a theory about your issue.

The error your getting indicates that you're setting headers twice. At first, I didn't see where that was happening, but I'm realizing now that the line

console.log('redirect', res.redirect('/'));,

_does_ actually call res.redirect(), which sends headers. This means that on the next line,

return res.redirect('/');

headers are being sent again, which isn't allowed. I believe this is where your error is coming from.

@DominusKelvin, always a good idea to take another gander at the docs. Good thought!

Thanks, @madisonhicks. It really works. I thought that res.redirect () works differently and it automatically redirects the user in the browser to the necessary page.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

visitsb picture visitsb  路  4Comments

randallmeeker picture randallmeeker  路  4Comments

radoslavpetranov picture radoslavpetranov  路  4Comments

kesavkolla picture kesavkolla  路  4Comments

Noitidart picture Noitidart  路  4Comments