Firebase multi-path update set a parent node to null

Viewed 280

I am trying to use firebase realtime database multi path updates. However trying to set a parent node to null as below will result on an error.

const firebaseUpdate = {}
firebaseUpdate[`user/${uid}`] = null
db.ref().update(firebaseUpdate) 

Error: Reference.update failed: First argument contains a path /user/USER_ID that is ancestor of another path /user/USER_ID/creationTime

I was wondering if there is a way to use multi-path updates in order to set a parent node with multiple children to null. I assume I could use remove or set function but I'd rather use the multi-path update.

1 Answers

The error message indicates that you're trying to apply two conflicting updates to the database in one operation. As the message says, your update tries to:

  1. write to /user/USER_ID
  2. write to /user/USER_ID/creationTime

The second one write is a child of the first one. Since the order of writes in a multi-location is unspecified, it's impossible to say what the outcome of the write operation will be.

If you want to replace any data that currently exists at /user/USER_ID with the creationTime, you should update it like this:

db.ref().update({
  "/user/USER_ID": { creationTime: Date.now() }
}) 
Related