Reputation: 1143
Using FireStore's REST API documented at: https://firebase.google.com/docs/firestore/reference/rest
I'm trying to create a new document in "users" that has a subcollection named "subcol1" with an empty doc in it called "subdoc1"
However, the following REST command to create the new document does not work:
curl -X POST
-H "Content-Type: application/json"
-d "{ 'fields': { 'subcol1': [{'subdoc1': { 'fields': {} } }] } }"
"https://firestore.googleapis.com/v1/projects/XXXX/databases/(default)/documents/users"
How does one add a subcollection to a new document (or existing one?) using FireStore's REST API?
I specifically have to use REST for this implementation.
This is the error response I get back:
{ "error": {
"code": 400,
"message": "Invalid value at 'document.fields[0].value' (Map), Cannot have repeated items ('subcol1') within a map.\nInvalid JSON payload received. Unknown name \"\" at 'document.fields[0].value': Proto fields must have a name.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document.fields[0].value",
"description": "Invalid value at 'document.fields[0].value' (Map), Cannot have repeated items ('subcol1') within a map."
},
{
"field": "document.fields[0].value",
"description": "Invalid JSON payload received. Unknown name \"\" at 'document.fields[0].value': Proto fields must have a name."
}
]
}
] } }
Thanks!
Upvotes: 2
Views: 1212
Reputation: 1143
After more tinkering with this, I found a solution.
The following works:
So first create the new "top-level" doc:
curl -X POST
-H "Content-Type: application/json"
-d "{ 'fields': { } }"
"https://firestore.googleapis.com/v1/projects/XXXX/databases/(default)/documents/users"
Then create the subdoc (which "creates" the subcollection):
curl -X POST
-H "Content-Type: application/json"
-d "{ 'fields': { } }"
"https://firestore.googleapis.com/v1/projects/XXXX/databases/(default)/documents/
users/<USER-KEY-FROM-PREVIOUS-JSON-RESPONSE>/subcol1"
Upvotes: 2