2016-12-06 59 views
0

我嘗試使用Firebase和Ionic構建聊天應用程序。Firebase沒有從數據庫獲得價值

如果我直接打開我的聊天頁面通過類似網址:

http://localhost:8100/#/home/messaging/jJ53KWgqnuWXSzksRYl1XNw20JJ2 

火力地堡似乎並沒有正常工作。但是,如果我從前一頁的菜單打開聊天頁面:

ui-sref="home.messaging({id: p.friendId})" 

一切工作正常。

這是我在ChatController代碼:

$scope.data.friendId = $stateParams.id; 

var refMessage = firebase.database().ref(); 

refMessage.child("user_profile").child($scope.data.friendId).once("value") 
     .then(function (profile) { 
      $scope.data.friendUserProfile = profile.val(); 
      //on debug this line is reached 
     }); 

refMessage.child("profile_photo").child($scope.data.friendId).once("value") 
     .then(function (profilePic) { 
      $scope.data.friendUserPhotos = profilePic.val(); 
      //I can't get friend's photos firebase doesnt reach this line. 

     }); 

refMessage.child("friends").child($localStorage.uid).child($scope.data.friendId).once("value") 
     .then(function (friend) { 
      if (friend.val() !== null) { 
       $scope.data.isNewFriend = friend.val().isNew; 
       //on debug this line is reached 
      } 
     }); 

var chatId = 'chat_' + ($scope.data.uid < $scope.data.friendId ? $scope.data.uid + '_' + $scope.data.friendId : $scope.data.friendId + '_' + $scope.data.uid); 

的問題是隻與朋友共享照片。 Firebase不會從數據庫中調用照片。所有其他調用都可以很好地實現異步,但從來沒有達到在調試時獲取照片的功能。

如果我刷新頁面,並且如果轉到上一頁並返回聊天頁面,一切正常,則會出現此問題。

我想如果我的$scope.data有問題。它不適用於以前的調用friendUserProfile,但它適用於它。

火力地堡v3.6.1

感謝

回答

0

經過與firebase.database.enableLogging(true);調試後,我已經找出了聽衆的東西。

在RootController上還有另一個呼叫refMessage.child("profile_photo"),如果我直接刷新聊天頁面,.once關閉該路徑上的所有偵聽器,並且我無法從ChatController的firebase調用中獲取數據。

1

由於火力點是asyncronous您使用的承諾來處理異步操作。這意味着您應該添加一個catch方法來查看Firebase是否返回錯誤。

如果您不知道,您將無法確定是否發生錯誤。

refMessage.child("profile_photo").child($scope.data.friendId).once("value") 
    .then(function (profilePic) { 
     $scope.data.friendUserPhotos = profilePic.val(); 
     //I can't get friend's photos firebase doesnt reach this line. 

    }).catch(function(error) { 
     console.log(error); 
    }); 
+0

謝謝我現在試試這個 – cgrgcn

+0

它沒有扔任何東西。就像等待Firebase響應一樣,不會從那裏返回。 – cgrgcn