2016-03-21 35 views
0

我在角度/離子2中使用firebase,並且需要檢查值是否存在以及如果不存在,則創建它但firebase不會返回null在docs狀態和我的支票沒有運行,因爲它。firebase .on值在不存在數據時不會返回null

subscribeToOffer(uid, offerID) { 
    var path = 'users/' + uid + '/offers/' + offerID; 

    this.rootRef.child(path).on('value', subscribed => { 
     if (subscribed.val() !== null) { 
      console.log('subscribed'); 
     } else { 
      console.log('not subscribed'); 
     } 
    }); 
} 
+0

如果它不返回null那麼它返回什麼?你確定這條路是正確的嗎?也許在你的問題中添加你正在使用的數據的樣本(作爲文本,而不是圖片) –

+0

它根本不會返回,沒有任何反應。路徑是正確的,因爲當報價存在時,它會運行,但是當它不是什麼都不做時,我需要返回執行我的else語句。 – Lindstrom

+0

你有沒有試過這個:https://www.firebase.com/docs/web/api/datasnapshot/exists.html –

回答

0

在這裏,我爲你寫一個簡單的函數,將在其他情況下,返回如果提議ID存在,

subscribeToOffer(userId,offerId){ 
     var userRef = new Firebase(FBURL+'users'); 
     var userOfferRef = userRef.child(userId).child("offers"); 
     return userOfferRef.on("value", function(snap) { 
      var offerIds = snap.val(); 
      return !!offerIds.hasOwnProperty(offerId); 
     }); 
    }; 
相關問題