无法获取网址参数

时间:2019-07-24 11:38:47

标签: javascript node.js url

我有一个网址:

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

在上述网址中,id6,而tokeneyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ

当我尝试使用req.params.id访问ID时,它会正确返回我值6

但是当我尝试使用req.params.token访问令牌时,它将返回undefined

我尝试使用JSON.stringify参数,然后将其打印出来,结果再次相同:

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

我的代码如下:

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")
    }
  }
});

有人可以告诉我我在这里犯什么错误吗?

注意:刚意识到我在url中传递的第二个参数始终是不确定的

0 个答案:

没有答案
相关问题