Attempting to create a redirection landing page

I have a process where I create a new user and save it in my database with the following code snippet:

const newUser = new User({
    username: userId,
    password: pass,
    nameOfUser: user_name,
    emailOfUser: user_email
);
newUser.save();

res.redirect("/redirectpage");
setTimeout(function(){
    res.redirect("/");
},5000);

Upon saving the user, I want to implement a countdown redirect page. It is visually represented like this:

https://i.stack.imgur.com/wc94c.png

Once the 5-second counter reaches 0, the user should be redirected back to the login page, which is the home route. I used setTimeout for this functionality. However, my application crashes and shows errors if I do not comment out res.redirect("/redirectpage").

https://i.stack.imgur.com/zkZfq.png

Answer №1

This particular snippet of code attempts to redirect the user twice within the same request - first to /redirectpage and then again to /. However, this is not possible due to the fact that calling res.redirect() for the first time will effectively complete the request, making it impossible to perform a second redirect. To resolve this issue, you should consider moving the redirect logic to the client-side javascript. One potential solution could look like this:

setTimeout(function() {
    window.location.replace("[full redirect url]");
}, 5000);

If you want to delve deeper into the topic of redirecting from the client-side, feel free to check out this informative answer.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to iterate through an ID using jQuery?

After pulling the list of doctors in my area from the database and displaying it on my webpage, I now want to load each doctor's "About" content inside a Bootstrap modal. I added an "about" column within the same database table where the doctors' ...

What could be the reason for receiving no file input after submitting the form?

One of the functions triggers the input file and displays previews: $(document).on("change", "#Multifileupload", function() { var MultifileUpload = document.getElementById("Multifileupload"); if (typeof FileReader != & ...

Instructions on returning information to the client following the acquisition of oauth details

The sequence of events: 1: client authentication via OAuth 2: redirect from the browser back to a predetermined URL with the client's code 3: server processing the code in the get method and obtaining the client's data My objective: Once the ...

NodeJs Express: The request cannot be posted

I'm facing an issue while trying to post a request. I made sure that I have set the urlencoded option to false. Every time I attempt to post with parameters, I keep getting "Cannot GET ....." on the webpage. Uncertain about the reason behind this er ...

The datatable does not adjust to changes in page size

My jsfiddle example showcases different card boxes, and I am currently focusing on making the 'Table 1' box responsive. However, I have encountered an issue: Check out my jsfiddle example here code If you open the jsfiddle with a large enough ...

iterating over a nested map within a map in an Angular application

I wrote a Java service that returns an observable map<k, map<k,v>> and I'm currently struggling to iterate through the outer map using foreach loop. [...] .then( (response: Package) => { response.activityMap.forEach((key: s ...

Persist in the face of a mishap in javascript

Two scripts are present on the page. If the first script encounters an error, the second script will not function properly as a result. Is there a way to make the second script ignore any errors from the first one and still work? Please note that I am str ...

Transferring data from JavaScript to ASP.NET (via AJAX)

I'm trying to figure out how to pass variables from JavaScript to a textbox. I have a variable in JavaScript but I am unsure how to transfer it to the textbox. I have heard that using AJAX makes it easy, but I don't know how to implement it. I th ...

determining the user's position within the <s:select> element

I have a dropdown select tag in my code with a list of countries. I want the user's country to be auto-selected when they access the page. This is the JSP code snippet: <s:select name="dropdown" list="countries" listKey="value" listV ...

Alter appearance using various classes

I am seeking assistance in changing the font of text using classes. I have multiple texts with different classes and I want to be able to edit all the texts without adding another dropdown menu. I believe that the change needs to occur in a script. Pleas ...

Leveraging React Native's Async Storage to store and retrieve values consistently within the Render method

Is there a way to set and get a value in the render method with just one line of code, by using a variable between tags? I attempted this approach but encountered an error message stating "Can't find variable: storage_key". import React, { Component } ...

Issue encountered during deployment of Vue.js application on Heroku platform

Having trouble deploying my vue.js/node.js/Snipcart application with Heroku. I kept encountering an application error despite successful local runs at http://localhost:8080. Any idea what might be causing this? Here are the console logs. Appreciate your he ...

How can I send parameters to an HTML file using Node.js?

In my current code res.sendfile('./home.html',{user:req.user}), I need to figure out a way to access the user parameter directly in the HTML file without relying on a template engine. Can anyone suggest how this can be achieved? ...

Identifying an anonymous function with a name (using .name versus .displayName)

In the context of my react native project, I have come across a function with an undefined name that is not being inferred. This function looks like: const f = function() {}; Despite maintaining its anonymous definition, there is an attempt to assign a na ...

I'm attempting to utilize a basic webcam capture upload feature, but it seems that the upload function is not functioning properly

UPDATE: This is the complete code that I simply copied and pasted. <!DOCTYPE HTML> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="JavaScript" type="text/javascrip ...

Guide to using AJAX for the GraphHopper Matrix API

I am struggling to send this JSON with the required information because I keep encountering an error during the request. message: "Unsupported content type application/x-www-form-urlencoded; charset=UTF-8" status: "finished" I'm not sure what I&apos ...

Having trouble locating a user's login information in Express with Passport

I have encountered an unusual issue with my app. It is successfully logging in using the user's Facebook credentials, but I am unable to retrieve their email, name, or ID. Here is the code snippet from my users.js route: router.get('/auth/facebo ...

Encountering difficulties with launching Jest following the installation of selenium-webdriver

In order to test my React application, I have implemented two types of tests. Jest is used for unit testing As I venture into feature testing, I decided to experiment with Cucumber @cucumber/cucumber. Initially, I considered using jest-cucumber, but it ha ...

Is there a way to merge two separate on click functions into one cohesive function?

I currently have two separate onclick functions as shown below. They are functioning properly but I am considering combining them for optimization purposes. Essentially, clicking on xx displays certain elements, hides others, and adds a class. Clicking o ...

Large Integer in Version 12 of Node (v12.22.12)

Trying to utilize BigInt with node.js version 12.22.12 has been proving challenging for me. While it operates smoothly in the REPL, I am encountering difficulties when trying to incorporate it into my script using the "require" function. I have attempted ...