【发布时间】:2018-07-22 01:23:28
【问题描述】:
我有一个干净的 url,其中包含一些像这样的查询参数。
我正在尝试像这样在客户端捕获查询参数“id”。
static async getInitialProps({req, query: { id }}) {
return {
postId: id
}
}
render() {
const props = {
data: {
'id': this.props.postId // this query param is undefined
}
}
return (
<Custom {...props}>A component</Custom>
)
}
我的快速端点看起来像这样。
app.post(
'/post/:id',
(req, res, next) => {
let data = req.body;
console.log(data);
res.send('Ok');
}
);
但我的服务器端控制台输出最终是这样的。
{ id: 'undefined' }
我已阅读文档和 github 问题,但我似乎无法理解为什么会发生这种情况。
【问题讨论】:
-
你检查
location.pathname了吗? -
是的,只是路径在那里 /post/B1L8VE0UF
-
我想我可以直接在里面插入 location.pathname 而不是使用查询。
-
我认为您需要根据执行环境同时使用两者。
标签: javascript express nextjs