我在 Firebase 上有一個“用戶”集合。這個集合中有一些我想在螢屏上呈現的欄位
我有一個包含以下功能的類 Home:
const db = firebase.firestore();
export class Home extends Component {
constructor(props) {
super()
}
componentDidMount(){
db.collection('/users')
.doc(firebase.auth().currentUser.uid)
.get()
.then(querySnapshot => {
querySnapshot.forEach(uid => {
let data = uid.data();
console.log(data);
})
})
}
}
如果沒有.doc(firebase.auth().currentUser.uid),我會得到螢屏上所有用戶的所有欄位,但是當我添加它時,為了獲取每個用戶的詳細資訊,我遇到錯誤“未捕獲(承諾)TypeError:querySnapshot.forEach 不是函式”。在此先感謝您的幫助。

uj5u.com熱心網友回復:
您正在使用get()一個DocumentReference的回傳一個DocumentSnapshot只包含單個檔案的資料并沒有任何forEach出現在它的方法。所以data()直接在快照上使用就可以了。嘗試重構代碼,如下所示:
componentDidMount() {
db.collection('/users')
.doc(firebase.auth().currentUser.uid)
.get()
.then((docSnapshot) => {
console.log(docSnapshot.data())
})
}
如果你想獲得的所有來自用戶的收藏中的檔案,然后使用get()上CollectionReference(基本上去掉.doc(uid))如下圖所示:
componentDidMount() {
db.collection('/users')
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.data())
})
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398211.html
標籤:javascript 火力基地 反应原生 谷歌云firestore
