Reputation: 371
I try to build chat application with Firebase and Ionic.
If I open my chat page directly via URL like:
http://localhost:8100/#/home/messaging/jJ53KWgqnuWXSzksRYl1XNw20JJ2
Firebase doesn't seem to work correctly. However, If I open chat page from previous page's menu with:
ui-sref="home.messaging({id: p.friendId})"
everything works fine.
This is my code on 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);
The problem is only with friends photos. Firebase doesn't call photos from database. All other calls works good async, but never reach to getting photos line on debug.
This problem occurs if I refresh the page and If go to previous page and come back to chat page everything works good.
I think if there is problem with my $scope.data
. It wouldn't work for the previous call which is friendUserProfile
, but it works for it.
Firebase v3.6.1
Thanks
Upvotes: 0
Views: 116
Reputation: 371
After debugging with firebase.database.enableLogging(true);
I've figured out something with listeners.
There is also another call for refMessage.child("profile_photo")
on RootController and if I refresh directly chat page, .once
close all listener on that path and I can't get data from my ChatController's firebase call.
Upvotes: 0
Reputation: 667
Since firebase is asyncronous you are using promises to handle the async operations. This means you should add a catch method to see if Firebase is returning an error.
If you dont, you will not be able to determine if an error occurs.
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);
});
Upvotes: 1