作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果有签名用户,我想使用 getServerSideProps 在 Firestore 中获取“示例”文档。下面的代码不起作用。结果是“无法读取”我应该怎么办?或者有其他方法吗?
export const getServerSideProps = () => {
let currentUser = []
authService.onAuthStateChanged(async user => {
if(user) {
const docRef = dbService.collection('whole_users').doc('sample').get()
await docRef.then((doc) => {
if(doc.exists) {
currentUser.push(doc.data())
}
})
} else {
console.log("can't read")
}
})
return {
props: {currentUser}
}
}
最佳答案
第一个:您在没有 await
的情况下调用 get()
。将您的代码更改为:
export const getServerSideProps = () => {
let currentUser = []
authService.onAuthStateChanged(async user => {
if(user) {
const docRef = dbService.collection('whole_users').doc('sample')
await docRef.get().then((doc) => {
if(doc.exists) {
currentUser.push(doc.data())
}
})
} else {
console.log("can't read")
}
})
return {
props: {currentUser}
}
}
第二个:onAuthStateChanged
仅针对客户端。要在服务器端访问身份验证状态,您需要将身份验证状态放入提供程序中。 Here是一个如何做到这一点的例子。
关于javascript - 如何在 nextJS getserversideprops 中使用 firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67625554/
我是一名优秀的程序员,十分优秀!