在我的 React Native 應用程式中,我使用 Firebase 獲得了一個值
this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)
我想記錄它回傳的值,但我不知道它是否回傳一個承諾。我想做類似的事情
let a = this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)
console.log(a)
我應該如何處理這個?
uj5u.com熱心網友回復:
目前,您只是對檔案的參考,而不是檔案本身。要一次獲取檔案,請使用.get. 這將回傳一個承諾:
this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)
.get()
.then(doc => {
console.log(doc.data());
})
// Or using async await:
const someFunction = async () => {
const doc = await this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)
.get()
console.log(doc.data());
}
或者,如果您想繼續偵聽更改,請使用onSnapshot:
const unsubscribe = this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)
.onSnapshot(snapshot => {
console.log(snapshot.data());
});
// Later you can call unsubscribe() to stop listening
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418405.html
標籤:
