【发布时间】:2021-09-11 13:14:22
【问题描述】:
当用户从 localhost:3000/ 连接时,我尝试从 Firebase 获取所有文档(它会自动重定向到 /profile),但它在第一次运行时不起作用。然后,当用户刷新页面时,它就可以工作了。我如何在第一次尝试时运行它?代码如下:
try {
const querySnapshot = await getDocs(collection(db, "links"));
await querySnapshot.forEach((doc) => {
if (stUser.uid == doc.data().uid) {
links.push(doc.id);
}
});
} catch (e) {
console.log(e);
}
重定向:
function first() {
if (!isLoggedIn.isLoggedIn) return <Redirect to="/auth" />;
}
function second() {
if (isLoggedIn.isLoggedIn) return <Redirect to="/profile" />;
}
return (
<div>
<Route exact path="/">
<Redirect to="/auth" />
</Route>
{handleRoute(images)}
<Route path="/auth" component={Dashboard}>
{second()}
</Route>
<Route strict path="/profile" component={HomePage}>
{first()}
</Route>
</div>
);
【问题讨论】:
-
links的信息如何在 UI 中呈现?您可能希望使用setState或useState钩子将其放入状态。查看其中一些:stackoverflow.com/…
标签: javascript reactjs firebase google-cloud-firestore