2016-12-06 48 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