gpt4 book ai didi

javascript - 用户路由器();在getServerSideProps :next js里面

转载 作者:行者123 更新时间:2023-12-05 00:34:46 24 4
gpt4 key购买 nike

我正在尝试在我的代码中使用 getgetServerSideProps 但我无法做到,因为我无法通过 router.query.itmid;

export async function getServerSideProps() {
// Call an external API endpoint to get posts.
const router = useRouter();
var id = router.query.itmid;
// You can use any data fetching library

const res = await fetch("https://ask-over.herokuapp.com/questone/" + id);
console.log("check");
console.log("dada");
const posts = await res.json();

// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
};
}
这是我尝试过的
https://codesandbox.io/s/youthful-lucy-427g1?file=/src/App.js
我是下一个 js 的新手,而且我对下一个 js 知之甚少,所以如果我理解文档我会问这个问题,请不要告诉我阅读文档

最佳答案

您应该在 getServerSideProps 中使用上下文来获取查询参数

export async function getServerSideProps(ctx) {
// Call an external API endpoint to get posts
var id = ctx.query.itmid;
// You can use any data fetching library

const res = await fetch("https://ask-over.herokuapp.com/questone/" + id);
console.log("check");
console.log("dada");
const posts = await res.json();

// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
};
}

关于javascript - 用户路由器();在getServerSideProps :next js里面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71030780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com