最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firebase - How use arrayUnion using firestore rest api? - Stack Overflow

programmeradmin4浏览0评论

I can't find a way to achieve the request even following the Firestore documentation.

I tried this request:

PATH (PATCH METHOD): /(default)/documents/error/AA/

BODY:

{
    "updateTransforms": [
        {
            "fieldPath": "kaja",
            "appendMissingElements": {
                "values": [
                    {
                        "stringValue": "Aasd"
                    }
                ]
            }
        }
    ]
}

RESPONSE:

{
    "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unknown name \"updateTransforms\" at 'document': Cannot find field.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "document",
                        "description": "Invalid JSON payload received. Unknown name \"updateTransforms\" at 'document': Cannot find field."
                    }
                ]
            }
        ]
    }
}

I can't find a way to achieve the request even following the Firestore documentation.

I tried this request:

PATH (PATCH METHOD): https://firestore.googleapis/v1/projects/social-autt/databases/(default)/documents/error/AA/

BODY:

{
    "updateTransforms": [
        {
            "fieldPath": "kaja",
            "appendMissingElements": {
                "values": [
                    {
                        "stringValue": "Aasd"
                    }
                ]
            }
        }
    ]
}

RESPONSE:

{
    "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unknown name \"updateTransforms\" at 'document': Cannot find field.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "document",
                        "description": "Invalid JSON payload received. Unknown name \"updateTransforms\" at 'document': Cannot find field."
                    }
                ]
            }
        ]
    }
}
Share Improve this question edited Feb 18 at 4:20 Frank van Puffelen 599k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Feb 18 at 3:23 Moises AvilaMoises Avila 31 bronze badge 3
  • You need to include the fields you want to update in the updateMask.fieldPaths in the URL, as shown here: stackoverflow/questions/48937981/… – Frank van Puffelen Commented Feb 18 at 4:23
  • Did you try Frank's suggestion? Does it work for you? – Alex Mamo Commented Feb 18 at 4:46
  • @FrankvanPuffelen bro, I already tried this: firestore.googleapis/v1/projects/social-autt/databases/… and it doesn't work for me either. – Moises Avila Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Based on the answer to How do I add/delete array elements on a Firestore document via REST? and the documentation for documents:commit, Write, DocumentTransform, and FieldTransform, I was able to execute this array union in Postman:

The JSON of the request:

{
    "writes": [{
      "transform": {
        "document": "projects/projectID.../databases/(default)/documents/79447051/test",
        "fieldTransforms": [
            { "fieldPath": "array",
              "appendMissingElements": {
                "values": [
                  { "stringValue": "third" }
                ]
              }
            }
          ]
        }
    }]
}

In this:

  • the blurred out bit is my project ID
  • the 79447051 is the collection ID (and your question ID here)
  • test is my document ID
  • array is the name of the array field that we're updating
  • third is the value we're adding to the array (if it's not already in there)

The response I got back from Firestore:

{
    "writeResults": [
        {
            "updateTime": "2025-02-18T21:06:02.185573Z",
            "transformResults": [
                {
                    "nullValue": null
                }
            ]
        }
    ],
    "commitTime": "2025-02-18T21:06:02.185573Z"
}
发布评论

评论列表(0)

  1. 暂无评论