[英]React-native and Firebase: Wait for Firebase data
我目前正在實現一個依賴Firebase數據的應用程序,然后它才能繼續。 但是,我經常(除非有意地等待)得到結果Cannot read property [property] of null
。 我非常懷疑這是因為在調用此對象之前無法發出firebase請求。
現在,我正在尋找實現集合點(或障礙)的方法,實際上是能夠實現在繼續之前接收所有Firebase數據的檢查點的任何方法。 Javascript中是否有什么可以幫助我做到這一點,或者有任何庫或任何本機庫可以幫助我實現這一目標?
我的代碼如下所示:(Fb是firebase接口)
@action
bookInterestedBike() {
this.bookedBikeNo = this.interestBikeNo;
this.downloadBikeObj();
this.updateBikeDataStartRide();
this.updateUserDataStartRide();
//
//This is where the firebase checkpoint / barrier should happen, and no progress should be made unless this data was fetched!
//
this.startTimer();
}
@action
downloadBikeObj() {
Fb.staticBikes.child(String(this.bookedBikeNo)).once('value', (bikeObj) => {
this.bikeObj = bikeObj.val();
console.log("Save object is: ");
console.log(this.bikeObj);
});
}
updateBikeDataStartRide() {
var updateVals = {}
updateVals[this.interestBikeNo] = {
bike_no: this.interestBikeNo,
current_user: "self",
positionLat: this.usrLat,
positionLng: this.usrLng
};
Fb.bikes.update(updateVals);
return false;
};
updateUserDataStartRide() {
var updateVals = {}
updateVals[this.uuid] = {
bike_no: this.bookedBikeNo,
uuid: this.uuid //TODO: remove this in deployment
};
Fb.users.update(updateVals);
return false;
};
在另一個組件,這是得到調用的函數(這是至關重要的是,所有的數據updateBookedBikeBegin()
被調用之前收到navigateToSuccessBookedBike()
updateBookedBikeBegin() {
this.userStore.bookInterestedBike();
this.navigateToSuccessBookedBike();
}
為了方便起見,在這種情況下,最好使用對回調的承諾。 您需要在downloadBikeObj
返回承諾。
downloadBikeObj() {
return Fb.staticBikes
.child(String(this.bookedBikeNo))
.once('value')
.then(bikeObj => {
this.bikeObj = bikeObj.val;
console.log("Save object is: ");
console.log(this.bikeObj);
}); // return promise
}
然后在bookInterestedBike
組成返回的promise。
bookInterestedBike() {
this.bookedBikeNo = this.interestBikeNo;
this.downloadBikeObj()
.then(() => this.updateBikeDataStartRide())
.then(() => this.updateUserDataStartRide())
.then(() => this.startTimer());
}
https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.