【问题标题】:Firebase doesn't get value from databaseFirebase 没有从数据库中获取价值
【发布时间】:2016-12-15 20:48:45
【问题描述】:

我尝试使用 Firebase 和 Ionic 构建聊天应用程序。

如果我直接通过以下 URL 打开我的聊天页面:

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

Firebase 似乎无法正常工作。但是,如果我从上一页的菜单中打开聊天页面:

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,但它适用。

Firebase v3.6.1

谢谢

【问题讨论】:

    标签: angularjs firebase ionic-framework firebase-realtime-database


    【解决方案1】:

    由于 firebase 是异步的,因此您使用 Promise 来处理异步操作。这意味着您应该添加一个 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);
        });
    

    【讨论】:

    • 它没有抛出任何东西。类似于等待来自 firebase 的响应并且不会从那里返回。
    【解决方案2】:

    在使用firebase.database.enableLogging(true); 进行调试后,我发现了一些与听众有关的东西。

    在 RootController 上还有另一个对 refMessage.child("profile_photo") 的调用,如果我直接刷新聊天页面,.once 会关闭该路径上的所有侦听器,我无法从 ChatController 的 firebase 调用中获取数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2018-06-23
      • 1970-01-01
      • 2017-07-04
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多