javascript - How to delete a specific child from a child in Firebase -
i have application user can create lists.
the user can share list other users. manage create sharing part, 1 i'm having issues delete part. want when user deletes list shared , list deleted other users.
this delete made list owner.
so scenario be:
- user creates list pushid = 1.
- this list added in following firebase ref: /userlist/$useraid/$pushid.
- user shares list user b , user c.
- this list added in following firebase ref: /userlist/$userbid/$pushid , /userlist/$usercid/$pushid.
- user deletes list pushid = 1.
so in
so have schema:
userlist: { 2xynkczfedpywfuj3e63yqedshe2: { -kt7lxiy0yt-odcv38l5 } ktqhkxmswksbyz1rmtrwjdmsyne3: { -kt7lxiy0yt-odcv38l5: {} -kt9xp91hjwcwgcbsgbc: {} } xhpmvorqccdzwtp70l29lza1ibd3: { -kt7lxiy0yt-odcv38l5: {} } }
in high level be:
userlist: { userid: (a) { -listid : (1) {} } userid: (b) { -listid: (1) {} -listid: (2) {} } userid: (c) { -listid: (1) {} -listid: (3) {} -listid: (4) {} } }
the current code have following:
const ref = firebase.database().ref('userlist'); ref.once('value') .then((snapshot) => { snapshot.foreach((childsnapshot) => { ref.child(childsnapshot.key) .once('value') .then((snapshot2) => { snapshot2.foreach((childsnapshot2) => { if (childsnapshot2.key === uid) { ref.child(childsnapshot2.key) .remove(); } }); }) .catch(() => { console.log('error2'); }); }); }) .catch((error) => { console.log(error); });
what i'm doing in code is, first fetching list inside userlist, getting key manage jump userid child. inside node once again manage jump inside pushid make validation of checking if current key equal uid of list want delete, if remove().
i feel there must better way of telling firebase go directly pushid , find of equal uid of list want delete , it.
there no way server-side delete of multiple items based on condition firebase database. must first retrieve items (or ids) matching condition , delete those.
however, can delete list , references in 1 go using multi-path update.
i'd recommend keeping list of uids you've shared specific list with, don't have loop on users. keeping many-to-many relations in both directions quite common in firebase , other nosql databases.
for more see:
Comments
Post a Comment