how to atomically add a new value to array field firebase

362 Views Asked by At

I want to atomically add a new value to the 'atrr' array field and read it

enter image description here

enter image description here

I use Pyrebase.

1

There are 1 best solutions below

0
Frank van Puffelen On BEST ANSWER

The picture you show is of the Firebase Realtime Database, yet in your code you're trying to use the firestore.ArrayUnion operator that only exists on Cloud Firestore. While both Realtime Database and Cloud Firestore are part of Firebase, they are completely separate and have different APIs.

There is no equivalent of the ArrayUnion operator for the Realtime Database. In fact, to store this structure on Realtime Database it is more common to do:

{
  "S": true,
  "M": true,
  "L": true,
  "XL": true
}

With this structure, you can add XXL to it with:

db.child("attr").update({ "XXL": true });