Reputation: 2062
In my app I create a default array of data upon user registration and add it under that user id in the following manner (using a service):
function addDefaultData(){
var defCategories = ['','data1',
'data2',
'data3',
'data4'];
var updates = {};
updates['/user-data/' + firebase.auth().currentUser.uid + '/' + 'categories'] = defCategories;
return firebase.database().ref().update(updates);
}
}
After registration the user can change this array, add or remove single item at a time. I read here but that does not help me. This is how the data looks in firebase db:
-appName-fomefirebaseid123
-user-data
-userID123687FHGJH4546HGJ
+5_2017
-categories
0: ""
1: "data1"
2: "data2"
3: "data3"
4: "data4"
How can I manipulate this array? Remove or add new items to it?
Upvotes: 0
Views: 24
Reputation: 2177
The Firebase team believes that "Arrays are Evil" (see: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html).
In that same article, they state: "Firebase has no native support for arrays. If you store an array, it really gets stored as an 'object' with integers as the key names."
I'd read the above blog and adjust my expectations and approach to interacting with Firebase accordingly.
In your case, if you want to delete index "0" of categories, you're really deleting key "0" of the categories document.
Upvotes: 1