Reputation: 59
I am creating a new user and saving it in my DB as shown below:
const user= new User({
username: userId,
password: pass,
nameOfUser: user_name,
emailOfUser: user_email
);
user.save();
res.redirect("/redirectpage");
setTimeout(function(){
res.redirect("/");
},5000);
After saving the user I am trying to create a countdown redirect page. It looks something like:
After this, counter (of 5 seconds) reaches 0, I intend to redirect the user to the login page which is the home route. I implemented this through setTimeout. But unfortunately my app crashes giving the following errors: (** Note: If i comment out res.redirect("/redirectpage"), no errors are shown)
Upvotes: 0
Views: 49
Reputation: 743
This code tries to redirect the user twice. First to /redirectpage
and then again to /
. You can't do this, because calling res.redirect()
the first time finishes the request, so you can't send any more data like a second redirect. You can fix this by moving the redirect code to the client side javascript. This code should work:
setTimeout(function() {
window.location.replace("[full redirect url]");
}, 5000);
Read this answer to learn more about redirecting from the client-side.
Upvotes: 1