0

enter image description here

Hello I am programming an app in android studio and i use firebase functions to delete all userdata when the USER gets deleted. Here in the photo when "asdf" from "USER" gets deleted the ID "asdf" should get deleted everywhere. This following code works fine but only if the ID isnt in the the directory of a unknown ID.

exports.deleteAdd1 = functions.database.ref('/USER/{userId}').onDelete((snap, context) => {

    const userId = context.params.userId;

    return admin.database().ref("/Add").child(userId).remove();
});

I really dont know how to delete the "asdf" under "Add" and another ID ...like in the picture (the ID "5wIddHUoJy" is unknown) Please help!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Marius
  • 31
  • 5
  • You will have to either 1) know each exact location to update, or 2) query for all those locations, iterate them, and delete them individually. Probably #2 is going to be the one for you. – Doug Stevenson Dec 18 '19 at 23:25

1 Answers1

0

The Firebase documentation states that data is stored as JSON Tree, this means that to retrieve a data node you need to provide the path under which it is located and that the operation will return every child node of the one you're asking for.

As Doug pointed out there are some options at your disposal:

  • You know all exact locations. To achieve this you may either store all ID-related locations in a separate document to get a kind of 1-to-many relation or use some kind of semantic naming if applicable to know locations straightaway.
  • Query then iterate then delete procedure. This one doesn't require to add/change database structure although it may be slow when requested nodes contain a ton of data.

Also you might want to take a look at this thread and particularly to this link about strategies for updating denormalized data.

Happy-Monad
  • 1,962
  • 1
  • 6
  • 13