How to use update mask to update a 'arrayValue' at an index?

Viewed 260

I need to update a particular field inside a document in Firebase. My application is WatchOS 6 application and hence I am sort of forced to use the REST API and uploadMask.

I have the following URL that points to the document that needs to be updated along with the field mask name that I need to update:

https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/users/{documentID}/goals/{documentID}/tasks/{documentID}?updateMask.fieldPaths=is_complete

Now, my request body:

{
  "fields": {
    "instructions&steps": {
      "arrayValue": {
        "values": [
          {
            "mapValue": {
              "fields": {
                "is_complete": true
              }
            }
          }
        ]
      }
    }
  }
}

I'll explain how I came up with the request body above:

The document has multiple values, one of them is fields. Inside fields, again I have multiple values. The one I want to update is 'instructions&steps' which is an array of JSON objects. I need to be able to update any index of that array and the field 'is_completed' on that particular index for my application.

I am not sure how to do that.

I tried figuring it out on the Google API Explorer with the above mentioned body and URL, I get a 400 error with message: document.fields[0].value.array_value.values[0].map_value.fields[0].value

Why does it say fields[0] ? That is not an array.

Ideally, it should be something like: document.fields.instructions&steps.array_value.values[0].map_value.fields.is_completed

And if I want to update for 2nd object, I simply need: document.fields.instructions&steps.array_value.values[1].map_value.fields.is_completed

Any help is appreciated. Thanks.

EDIT 1:

Okay, so I was able to get the request working by changing "is_complete": true to "is_complete" : {booleanValue: true}. I now get a 200 status and the entire document in response, but the value of field is_complete is still false and isn't updated! Additionally, how do I specify the index of the JSON at which I want to update is_completed field ?

After doing some additional digging, apparently Firebase doesn't allow you to update individual values in arrayValues yet. Is this true ?

If so, is there some way to work around that since my platform(Apple Watch/ watchOS 6) doesn't have Firebase SDK yet and I have to use REST API because of that.

0 Answers
Related