I'm tryin to achieve a function which will return all rooms of specific user. I'm working with firebase
and to get a specific room I wrote this function:
Db.prototype.getRoom = function (roomUid){
return new Promise(function(resolve, reject) {
firebase.database().ref('rooms/' + roomUid).once('value').then(function(snapshot){
resolve(snapshot.val());
})
});
}
Here is my problem - I need to download all rooms before returning the array. I've tried:
Db.prototype.getUserRooms = function(user){
var rooms = [];
firebase.database().ref('users/' + user.uid + '/rooms')
.once('value').then(function(snapshot){
var roomsIds = snapshot.val() || []; //array of rooms ids
for(var i = 0; i < roomsIds.length; i++){
Db.prototype.getRoom(roomsIds[i]).then(function(room){
rooms.push(room);
});
}
return rooms;
});
}
Thanks for any help