我正在使用Node.js,我想看到已经发布到我的脚本的所有参数.为了实现我的功能,在我的routes/index.js工作中:
app.post('/v1/order', order.create);
Run Code Online (Sandbox Code Playgroud)
然后在我的功能中,我有:
exports.create = function(req, res, next) {
console.log( req.params );
Run Code Online (Sandbox Code Playgroud)
但它返回一个空数组.但当我这样做时:
exports.create = function(req, res, next) {
console.log( req.param('account_id') );
Run Code Online (Sandbox Code Playgroud)
我得到数据.所以我对这里发生的事情感到有些困惑.
Mar*_*ahl 29
req.params只包含路由参数,而不是查询字符串参数(来自GET)而不是正文参数(来自POST).然而,param()函数检查所有三个,请参阅:
http://expressjs.com/4x/api.html#req.params
Edd*_*ddy 24
req.params
只能在这种模式下获取请求URL的参数:/user/:name
req.query
得到查询params(name)like/user?name=123或body params.
Sco*_*ger 12
我有一个类似的问题,并认为我会为那些出于同样原因来到这里的人发布解决方案。我的 req.params 是一个空对象,因为我在父路由中声明了 URL 变量。解决办法是在路由器上添加这个选项:
const router = express.Router({ mergeParams: true });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23345 次 |
| 最近记录: |