我试图从Firebase数据库中检索数据,并从Firebase存储中检索相应的图片。问题是,我的观点不想用数据更新自己。更新后的范围变量未显示在视图中。承诺无效
如果我试图简单地从我的数据库中获取数据,它可以很好地工作。一旦我添加获取图片的功能(需要稍微长一些),它看起来像我的视图只是立即查看范围变量,并不等待$ scope.friendsinfo更新。我认为我在承诺中做错了什么,应该使用$ q,但我不知道具体如何。谁能告诉我什么是最好的方式去做这件事?非常感谢!
var friendsRef = firebase.database().ref('friendships/' + firebase.auth().currentUser.uid);
$scope.friends = $firebaseArray(friendsRef);
$scope.friendsinfo = [];
$scope.$watch('friends', function() {
var newfriends = $scope.friends;
var newfriendsinfo = [];
for(var i = 0; i < newfriends.length; i++){
var ref = firebase.database().ref('users/' + newfriends[i].$id);
var profilePicRef = firebase.storage().ref("profilepictures/" + newfriends[i].$id + "/profilepicture");
var picPromise = fetchPicture(profilePicRef);
var newfriendid = newfriends[i].$id;
var newfriendagreed = newfriends[i].agreed;
picPromise.then(function(data){
ref.once('value', function(snapshot){
newfriendsinfo.push({
id: newfriendid,
name: snapshot.val().name,
email: snapshot.val().email,
agreed: newfriendagreed,
profilepicture: data //This is the functionality that causes my view to not display the updated $scope.friendsinfo because it takes too long.
});
});
});
}
$scope.friendsinfo = newfriendsinfo;
alert($scope.friendsinfo.length);
}, true);
function fetchPicture(ref){
return ref.getDownloadURL().then(function(url) {
return url;
}).catch(function(error) {
alert("error");
});
}