Home > Software engineering >  Firestore document not incrementing using REST API
Firestore document not incrementing using REST API

Time:03-22

I am trying to increment my Cloud Firestore using the REST API, however the documentation is unclear as to how this works. Here is my current implementation:-

I am trying to increment the integer value 'c' by 2, so if server value stored is 10 I want it to become 12 with this. Please help.

{
  "writes": [
 {
        "currentDocument": {
          "exists": true
        }
      },
      {
        "transform": {
          "document": "projects/project_name/databases/(default)/documents/collection_name/user_id",
          "fieldTransforms": [
            {
              "increment": {
                "c": {
                  "integerValue": "2"
                }
              }
            }
          ]
        }
      }
  ]
}

Error I keep getting:-

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"\": Root element must be a message."
          }
        ]
      }
    ]
  }
}

Edit: Here's the error after fixing my payload:

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

CodePudding user response:

As per the enter image description here

For more information, you may want to check Firebase REST API: Write.

  • Related