Home > front end >  Firestone create document with fields at root with REST API
Firestone create document with fields at root with REST API

Time:10-16

i am trying to make an API call to create a document inside of my “Users” collection and include an “Age” and “Name” as fields. Unfortunately when I make the API call I create Users>document>collection>document>info I would like to omit: Users>document>collection>document>info but the API won’t let me. Refer to the image below. enter image description here

Currently my code looks like this:

POST https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users/hello/APITest HTTP/1.1

Accept: application/json
Content-Type: application/json

{
  "fields": {
    "Name": {
      "stringValue": "Josh"
    },
    "Age": {
      "integerValue": "23"
    }
  }
}

I have removed some portions.

Any suggestions on how to make this work?

CodePudding user response:

Looking at your current target URL:

https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users/hello/APITest

You are trying to write data to /Users/hello/APITest, which is the path of a collection, not a document. Because you haven't provided a document ID, Firestore generates one for you and creates a new document in that collection at:

/Users/hello/APITest/{automaticDocumentId}

To prevent this you need to change your path to that of a document, such as /Users/hello, which makes your target URL:

https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users/hello

Alternatively, you can let Firestore generate an ID for you by using just /Users and the target URL:

https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users

CodePudding user response:

The http POST request path consists of two required path parameters one is parent and another is collectionId. In your case the http POST request is using the second type parent path parameter which is mentioned in the OR section of the parent path parameter here i.e.

projects/{project_id}/databases/{databaseId}/documents/chatrooms/{chatroom_id}.

So in your case the parent path parameter is : projects/###/databases/(default)/documents/Users/hello and APITest is the collectionId. Therefore Firestore is creating a nested collection i.e. a collection Users within which a document hello and within that another collection APITest and within that a document having an auto generated document id.

So to have the field values in the root document you have to specify the parent path parameter as : projects/{project_id}/databases/{databaseId}/documents and the collectionId path parameter as Users. It will create a collection with collectionId Users and within that a document with an auto generated document id.

So in your case the http post request will look like the following -

https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users

Please note that if you try to use https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users/hello it will throw an invalid argument error as the path parameters mentioned here are not meeting the Firestore http POST request path parameters requirement as mentioned in the above referred document.

Instead if you want to use your custom document id you can use the query parameter as mentioned here. So your http request will look like following -

https://firestore.googleapis.com/v1/projects/###/databases/(default)/documents/Users/?documentId=custom_document_id
  • Related