0

Long time, first time. I am new to learning node JS, Javascript, express, etc. and have been dealing with a problem for three days and counting. Hoping for some kind help here.

I have a view (called 'standings' view) where the user fills in an HTML form entering their username and bunch of team names.

  • I handle the post request using express and save the data in mongodb
  • The request contains a username field which can be referenced by 'req.body.username' inside the express app.post method.

What I am trying to do is capture this 'req.body.username' from the POST request and use it in another JS file (called 'thankyou.js') which would be the config file for a "thankyou" view. In other words, I am looking for a way to somehow reference the 'req.body.username' parameter directly without having to go back to the DB and in my "thankyou" view I want to say something like:

"Thank you" + <%= req.body.username %> + "for contacting us".

What would be the best approach to accomplish this? I have thought of and researched the following but not quite sure how to implement:

  • Somehow export 'req.body.username' property in my standings JS file and require/reference it in the thankyou JS file.
  • Using query strings to pass the 'req.body.username' in standings.js to the thankyou view.

Here is my express app.post method which handles the POST call from the user for the 'standings' view:

app.post('/standings', urlencodedParser, function(req, res){

var newStandings = standingsModel.update({ username: req.body.username}, req.body, {upsert: true}, function(err,data){
  if (err) throw err;

res.json(data);
})
});

Thanks in advance.

tshayan
  • 3
  • 2
  • If you want to use the value some time later (not in the same request that you retrieved the value in), then you have to store it somewhere that is per-user so you can get it in the later request by that user. A common place to put it might be in a session object that is per-user. Without understanding more about where you get the value and then were you want to use the value, we can't really comment in any more detail. – jfriend00 Jun 11 '17 at 16:15
  • I never really thought of using session objects. I'll look into it.... My initial idea was to save the 'req.body.username' value in a javascript variable, then export the variable and use it in the "thankyou" JS file... but I couldn't implement the export / require code to do it. Does this approach make any sense? – tshayan Jun 12 '17 at 13:55

1 Answers1

0

Are these REST calls? It looks like it, so I'm going to assume that this is stateless and you can't use session, right?

When do you send the user to the "thank you" page? If it's after submission, can you do a redirect instead of making everything an AJAX call?

I would take a look at this answer on passing data between routes/pages in ExpressJS. See if that offers any value to you. You might have to make some architectural changes.

  • The user gets the thank you page after submission via res.redirect('/thankyou') in the express app.post method. Problem is after the redirect the value of the initial request (req.body.username) is lost. So if I could find a way to save the 'req.body.username' value in a javascript variable in standings.js, then export the variable and use it in the "thankyou" JS file I think it would serve the purpose. I'll take a look at the link you posted. Thanks. – tshayan Jun 12 '17 at 14:00
  • I would look into using session or the flash library. The link I posted should do the trick for you. –  Jun 12 '17 at 22:14
  • 1
    I ended up getting this to work by using query strings. But I think I should spend some time and learn about sessions in order to do this properly. I am going to accept your answer because the link you provided guided me in the right direction significantly. Thanks for the help. – tshayan Jun 14 '17 at 13:54