My title doesn't quite capture the issue I'm facing.
I've been delving into firebase and angularfire, learning how to work with data manipulation.
Everything was going smoothly until I encountered this peculiar array structure.
https://i.sstatic.net/kFdxh.png
This is my real-time Firebase database containing a history of sorts.
The main array represents the history itself, consisting of two sub-arrays (0 and 1) corresponding to user 0 and user 1's histories.
User 0's array includes their user ID (_ID) and two sub-arrays (0 and 1) indicating the levels they have completed.
Each sub-array records the number of times a level has been completed, the level's ID (_id), and additional sub-sub-arrays with data for each time the level was run.
Quite complex, isn't it? I hope you grasp the structure of my database. Admittedly, I may not have adopted the best practices in structuring my database.
My current challenge involves searching through these arrays for my AngularJS application.
For instance, I'm striving to create these variables:
$scope.history
$scope.userHistory
$scope.currentLevelUserHistory
$scope.lastRunCurrentLevelUserHistory
I've successfully retrieved the first two variables but can't seem to understand why the latter ones are failing.
Fetching history is simple; I just use this function in my factory:
getHistoriques : function () {
return historiques;
}
Where "historiques" points to:
var ref = firebase.database().ref().child("historiques");
var historiques = $firebaseArray(ref);
Acquiring userHistory posed more of a challenge.
I devised this search function:
function search(key, array){
console.log("Searching array with the key: " + key);
for (var i=0; i < array.length; i++) {
if (array[i]._id === key) {
console.log(array[i]);
return array[i];
}
else {
}
}
}
And the result is an object like this:
https://i.sstatic.net/yAJMG.png
However, progressing to the next step—currentLevelUserHistory—proved elusive. Despite trying ten different methods, I couldn't get it to work.
My final attempt involved injecting the output from the previous object back into the search function by adding the level ID.
Unfortunately, all I received was "undefined."
I experimented with various approaches, including adding [0] to my object, among other modifications. Yet, I remain puzzled as to why nothing seems to work. To me, the array and its sub-array appear unchanged.
Why is it failing? Could it be that the function's output is altering my array?
I realize my query is lengthy. Many thanks to those who took the time to read through it.
Thank you kindly.
First EDIT:
users
|
- userId (according to firebase auth)
|
- name: ...
|
- surname: ...
|
- age: ...
|
- allowed levels:
|
- allowedLevel001: levelId
|
- allowedLevel002: levelId
|
- allowedLevel003: levelId
-...
levelsData
|
- levelId
|
- level name: ...
|
- level description: ...
|
- first step
|
- step name: ...
|
- step desc: ...
|
- action needed: ...
|
- time: ...
- second step
|
- step name: ...
|
- step desc: ...
|
- action needed: ...
|
- time: ...
|
-...
levelsRunData
|
- userId+levelId
|
- run001
|
-
first step result:
|
- ...
second step result:
|
- ...
|
-...
- run002
|
-
first step result:
|
- ...
second step result:
|
- ...
|
-...