问题
I have an application where there are a couple of weird issues.
*** UPDATED BELOW ***
We're using Express/Node with Postgres DB. When I start both servers up, everything works fine, as expected. When I open up Chrome and start to input the URL:port, we immediately get log information before ever pushing the Enter key. It's here where everything returns the expected values. When I press enter, it redirects to an /auth page, grabs a token and returns it, but this is where it weirds out:
Typically, when it's a fresh server restart, it will get down to the res.redirect(url)
call and then it will stop. It doesn't reach back to the database and grab all the requisite JSON data it should be pulling. If I repeat the process two (or three or four...) more times, it will grab all the information and allow me to update my user record. Additionally, if I stop/restart the servers, enter URL and press enter and then refresh the page, everything works as expected.
*** I forgot to mention that this problem didn't rear its head until we started making a call to get the token. If I take out the code to retrieve the token, I don't have this problem, leading me to think it's a timing-based issue, but I can't find where the problem is after a long time testing. ***
Is there any known issue as to why the redirect will sometimes work and other times not?
Involved scripts below:
home.js
router.get('/', function(req, res) {
res.redirect(GET_TOKEN) });
router.get('/auth', function(req, res) {
auth_code = req.query.code
userJson = {}
userJson.full_name = 'John Doe'
userJson.email = 'johndoe@example.com'
userJson.emp_id = 12345678
userJson.app_id = 'doejohn'
userJson.is_admin = true
req.session.user = userJson
var url = '/reg'
if (!auth_code || auth_code === undefined) {
url = '/'
}
console.log("Attempting redirect")
res.redirect(url) });
registration.js
router.get('/', async function(req, res) {
userJson = req.session.user
userJson.user_id = ""
userJson.user_name_cd = ""
userJson.user_password = ""
userJson.existing_user = false
params = [userJson.app_id]
var url = 'user/' + params + '/info'
exstngRegs = await util.httpGet('exstngRegs', url)
rolecode = 'A'
userJson.rolecode = rolecode
user_name_cd = ''
button_name = 'Register'
view_name = 'registration'
if (JSON.stringify(exstngRegs) != '[]') {
userJson.existing_user = true
button_name = 'Update Registration'
userJson.user_id = exstngRegs[0].user_id
user_name_cd = 'readonly'
if (!userJson.is_deleted) {
view_name = 'regexists'
} else {
view_name = 'registration'
button_name = 'Register'
}
userJson.user_name_cd = user_name_cd
userJson.button_name = button_name
util.handleView(res, constants.registration_title, constants.registration_menu, view_name, userJson) });
What ends up happening on a failure is I'll get a ReferenceError on some userJson information (usually, but not always, on the ** user_id ** (which is an input typed in at user registration and is verified in the database and tied to the correct user).
Just boggling my mind why it would show me a console log indicating that it's attempting a redirect, but not moving on from there and going to the registration.js file. Any ideas? Thanks in advance!
UPDATE FOLLOWS
Looking more at this code, I think I see what's happening, but not sure how to fix it. When I begin typing my URL, all the data loads (console logging shows connection with the database and I have logs displaying the web app is running as expected)--all this happens and is shown in the terminal BEFORE I press the Enter
key. Essentially, everything is good to go RIGHT NOW. But, when I press the Enter
key to confirm that, yes, I want to go to this web page, it runs a GET /, which redirects to the /auth page and runs JUST that code up to, but not including, the res.redirect(url)
.
Does anyone know a fix for this, or how to enforce that res.redirect(url) to run? I tried creating a simple function that runs prior to it and throwing that redirect in there, but no dice. Help!
来源:https://stackoverflow.com/questions/65429679/res-redirecturl-only-working-intermittently