I have an application where a user can create lists.
The user can share a list with other users. I already manage to create the sharing part, the one I'm having issues is with the delete part. I want that when a user deletes a list that is shared , this list is also deleted from the other users.
This delete will be made only by list owner.
So a scenario will be:
- User A creates a List with pushID = 1.
- This list is added in the following firebase ref: /userList/$userAID/$pushID.
- User A shares list with User B and User C.
- This list is added in the following firebase ref: /userList/$userBID/$pushID and /userList/$userCID/$pushID.
- User A deletes list with pushID = 1.
So in my
So i have this schema:
userList: {
2xYnKcZFEdPYWfUJ3E63yQEDShe2: {
-Kt7lXiY0Yt-oDcV38L5
}
KtQHkXMSwKSByZ1rmTRwjDmSYnE3: {
-Kt7lXiY0Yt-oDcV38L5: {}
-Kt9XP91hjwcwgcBSgbc: {}
}
XHpMVoRqcCdzwTP70L29Lza1ibD3: {
-Kt7lXiY0Yt-oDcV38L5: {}
}
}
In high level this will be:
userList: {
userID: (A) {
-listID : (1) {}
}
userID: (B) {
-listID: (1) {}
-listID: (2) {}
}
userID: (C) {
-listID: (1) {}
-listID: (3) {}
-listID: (4) {}
}
}
The current code I have to do this is the 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 this code is, first fetching ALL the list inside userList, by getting the key I manage to jump to the userID child. Inside this node once again I manage to jump inside the pushID where I make a validation of checking if current key is equal to the UID of the list i want to delete, if so I do a remove().
I feel there must be a better way of telling Firebase to go directly to pushID and find all of those that are equal to the UID of the list I want to delete and then do it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…