Recently, I've been delving into Firebase and despite my solid grasp on it, I still find myself encountering unexpected challenges. My background in SQL has made adapting to NoSQL quite the learning curve.
The Goal:
My objective is to fetch user profile data whenever an end user selects a category from a dropdown menu.
Data Structure:
Within my "userProfiles" collection, there's a nested collection of categories that users can select for themselves. You can see how it looks like in this image: https://i.sstatic.net/mxpXW.png
There could be countless user profiles with various nested categories.
The Code. Attempts Made:
Initially, I had a simple $firebaseArray setup which was functional but seems to have stopped working now for some reason.
Attempt #1
Please note that "category" here represents a unique id ($id)
<!-- @ref = https://XXXX.firebaseio.com/userProfiles/-KHZmwpSwtI1GK5XyXnu/categories -->
var ref = fbRefDataService.userProfiles.orderByChild("categories").equalTo(category);
var fbArray = $firebaseArray(ref).$loaded(); // I have treid with loaded and without
$scope.userData = fbArray;
Attempt #2
var ref = fbRefDataService.userProfiles;
ref.orderByChild("categories").equalTo(category).on("value", (snapshot) => {
snapshot.forEach(function(childSnapshot) {
console.log(childSnapshot.val());
});
});
Attempt #3
var qProfile = $q.defer();
var ref = fbRefDataService.userProfiles.child("categories");
ref.orderByChild("$id").equalTo(category).on("value", function(snapshot) {
qProfile.resolve(snapshot.val());
}, function(errorObject) {
qProfile.reject(errorObject);
});
return qProfile.promise;
This quest has kept me occupied for days now, exhausting all my known solutions. Surely, I must be overlooking something simple.
I'd truly appreciate any insight or guidance towards retrieving user data based on category selection.
Additional Information:
I'm utilizing Ionic 1.3 and AngularJS 1.5.5 for this project.
Thank you immensely for any assistance provided.