2

I am trying to pass parameter from HTML form to NodeJS router. Routing works as expected but i get undefined for parameter value.

I have done it in a past with parameter being a value of input field. However, this time I am trying to do without fields and send parameter as part of action statement.

<form id="form_sidebar" action="/users/navigation?user_id=UserID" method="POST"> 
  <button id = "btn_button" type='submit'>Button</button>
</form>

on router side:

router.post('/navigation', (req, res) => {
  var fname =  req.body['user_id']
  console.log(fname)
})

I was expecting to get UserID printed in the console. But getting undefined.

Any input will be greatly appreciated.

1 Answer 1

4

Simply you are not accessing the right part of the request since you are passing user_id as query parameter, not inside the request body, so Express seralizes an object with the query parameters. Try to access the parameter in this way:

router.post('/navigation', (req, res) => {
    var fname = req.query.user_id
    console.log(fname)
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.