如果有签名用户,我想使用 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}
}
}
首先:
你打电话get()
没有await
。将您的代码更改为:
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 https://colinhacks.com/essays/nextjs-firebase-authentication是一个如何做到这一点的示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)