1

I am building a resend verification email button for the logged in users. My plan is to let the user click the button on the profile page, and it will send the email directly to the user's email address.

It seems that I am stuck with sending the user's data to the backend code when clicking the button.

I got undefined when I check the req.params.id in the route setting.

Here is my route:

//route for requsting verification email
router.post("/verify", middleware.isLoggedIn, async(req, res) => {
    console.log(req.params.id);
})

Here is the ejs/html:

<form action="/verify" method="POST">
   <input type="submit" class="btn btn-success btn-sm" value="Verify">
</form>

I think my html code needs some onClick event to explicitly send the user's id to the server code in order to find the correct user. However, I have no clue how to make this. Thank you in advance for any help!

UPDATE Thanks to @Mizibi for helping me understand the difference between req.params and req.body.

SO what I did is to add another hidden input tag in the form like this:

<form action="/verify" method="POST">
     <input type="hidden" name="id" value="<%= currentUser.id %>" >
     <input type="submit" class="btn btn-success btn-sm" value="Verify">
</form>

And I changed the backend req.params.id to req.body.id, This will pass the currentUser's id to the backend code for finding the user and do the operations.

Kevin Chan
  • 148
  • 9

1 Answers1

1

The parameter id is not sent by the form.

Input doc

<input name="id" type="text" value="youridvalue" />

And you can retrieve it from back-end with :

req.body.id

Ensure to have express.bodyParser() used in order to be able to access body in request

FYI : req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?' Hanfei Sun

Mze
  • 197
  • 9
  • Thank you! That is very helpful! I will update what I did in my code in case anyone else has the same issue. – Kevin Chan Sep 20 '20 at 18:44