我正在尝试让动态路由工作,以便使用 url 中的 id 显示和检索正确的信息。当我访问该页面时,这有效,但当我重新加载页面时,ID 为空。这可以修复吗?我在互联网上找不到答案。
任何帮助,将不胜感激。
参考代码
//Initializing router
const router = useRouter();
//Getting id from url
const { id } = router.query;
//Fetching postdata
const getPostData = async () => {
setLoading(true);
await db
.collection("Posts")
.doc(id)
.get()
.then(function (doc) {
if (doc.exists) {
setPostData(doc.data());
setLoading(false);
} else {
//router.push("/404");
}
})
.catch(function (error) {
//router.push("/404");
});
};
Run Code Online (Sandbox Code Playgroud)