summarize Values und Multiple path Update Firebase Realtime Database with Typescript

Viewed 21

I've been trying to update a simple map with different entries.

I created first the const

const dMap: { [key: string]: any } = {};

and adding my Values:

dMap[urlToCurrentEvent] = updateMap

My Update Map show like :

const updateMap = {
  [new table.DB_artikel().anzahlVerfugbar()]: bestand,
  [new table.DB_artikel().visible()]: visible,
}

when i show it in console look like :

'karte/aktuell/34/artikel_inventar/34': { anzahlVerfugbar: 0, visible: false }, 'karte/aktuell/34': { anzahlVerfugbar: 0, visible: false }

Thats fine and it seems to work, when i have different Paths, but now coms an error in updating Firebase :

Error: update failed: values argument contains a path /karte/aktuell/34 that is ancestor of another path /karte/aktuell/34/artikel_inventar/34

I am understood , but was is the resolution?

1 Answers

You're trying to perform these two updates in one go:

'karte/aktuell/34/artikel_inventar/34': { 
  anzahlVerfugbar: 0, 
  visible: false 
}
'karte/aktuell/34': { 
  anzahlVerfugbar: 0, 
  visible: false 
}

But that first path is a child of the second path, so the order of the updates suddenly matters here - and that is not allowed in a multipath update.

To make this explicit, if we execute the updates in the order above, we end up with:

karte: {
  aktuell: {
    '34': { 
      anzahlVerfugbar: 0, 
      visible: false 
    }
  }
}

But if we execute them in the reverse order, we end up with:

karte: {
  aktuell: {
    '34': { 
      anzahlVerfugbar: 0, 
      visible: false,
      artikel_inventar: {
        anzahlVerfugbar: 0, 
        visible: false
      }
    }
  }
}

Since this means the update becomes non-deterministic, the data rejects the operation.

If you want that last result, you should send a single path update:

'karte/aktuell/34': { 
  anzahlVerfugbar: 0, 
  visible: false,
  artikel_inventar: { 
    '34': {
      anzahlVerfugbar: 0, 
      visible: false 
    }
  }
}

I'd also recommend reading the Firebase documentation on structuring data, specifically the section on avoiding nests, as this is a common cause of these problems.

Related