0

I have a url:

http://localhost:3000/api/user/passwordreset/6/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ

In above url, id is 6 and token is eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ

When I am trying to access the id using req.params.id it properly returns me value 6

But when I am trying to access the token using req.params.token it returns undefined.

I tried JSON.stringify the parameter and then printed it, the result is again same:

req.params: 
 {"id":"6","token":"undefined"}

My code is as follows:

router.get('/passwordreset/:id/:token', async (req, res) => {
  console.log(`req.params: \n ` + JSON.stringify(req.params))
  let id = req.params.id;
  let token = req.params.token;
  console.log(`id from get request: ${id}`)
  console.log(`token from get request: ${token}`)
  const result = await helper.checkIdExists(id)
  if (result === null) {
    let error = {}
    error.isError = true
    error.message = 'User id did not exists'
    res.status(500).json(error)
  } else {
    console.log("id check is successful")
    console.log("-----------------------------------------")
    // Decrypt one-time-use token using the user's
    // current password hash from the database
    console.log(result)
    console.log('$$$$$$$$$$$$$$$$$')
    let secret = result[0].password
    console.log(secret)
    console.log(token)
    console.log(req.params.token)
    var payload = jwt.decode(token, secret);
    console.log(payload);
    if (typeof payload === "object") {
      //TODO: Password change web link
      res.redirect()
    } else {
      res.status(400).send("id / token is incorrect")
    }
  }
});

Can someone please tell me what mistake I am making here?

NOTE: Just realised that whatever second parameter I am passing in url is always undefined

D555
  • 1,704
  • 6
  • 26
  • 48
  • Does any of this help? https://stackoverflow.com/questions/33236916/nodejs-not-able-to-get-token-value-from-req-params-token – Joss Classey Jul 24 '19 at 11:46
  • are you sure url you are hitting is `http://localhost:3000/api/user/passwordreset/6/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ` have you tried with postman? as the route looks fine. – AZ_ Jul 24 '19 at 11:50
  • yes, i am hitting correct url.. whatever i console.log it appears in the console.. so its the right page – D555 Jul 24 '19 at 11:51
  • you check if you have not added any space when defining router. – AZ_ Jul 24 '19 at 12:06
  • may be your get request limit is exceeded. – Shubham Dixit Jul 24 '19 at 12:20
  • @Shubh request limit ? the entire url is under 200 characters – D555 Jul 24 '19 at 12:35
  • https://stackoverflow.com/questions/52751383/express-param-length-limit-for-get ,@D555 have a look – Shubham Dixit Jul 24 '19 at 12:36
  • Does it work if you pass some short simple value like `/api/user/passwordreset/6/test`? – GProst Jul 24 '19 at 20:43

0 Answers0