【发布时间】:2018-06-29 06:15:22
【问题描述】:
在我的 index.js 中,我以这种方式给出了路由。
app.use('/users/:id/transactions',transactionRoutes)
事务路由内部
router.get('/:txnHash',transactionController.getTransaction);
所以对 '/users/:id/transactions/:txnHash' 的请求将到达上述路由。
事务控制器内部
module.exports.getTransaction = (req,res) => {
let typeOfTransaction = req.query.type,
userId = req.params.id,
txnHash = req.params.txnHash;
}
这里我可以访问 txnHash 参数,但 userId 参数显示未定义。我认为这是因为路由的 :id 部分是在 index.js 中指定的。有什么方法可以在不改变路线的情况下解决这个问题。
API 请求是
GET 'apiurl/users/42342234/transactions/234bh2428b354hjcs'
【问题讨论】:
-
能否提供已发送的请求
-
试试
req.param('id')看看它是否有效。 -
@GobuCSG 请检查新的编辑。
-
@DavidR 尝试过,但它显示下面给出的消息 express deprecated req.param(name): Use req.params, req.body, or req.query instead
标签: javascript node.js express routing