Firestore Error: Unable to create document inside a nested collection with REST API - rest

My firestore DB structure: 2020/abhishek/friend-requests/ where 2020 is a collection, abhishek is a document and friend-requests is a collection inside the document.
What am I trying to do?
I'm trying to create a new document inside friend-requests using REST API.
What I did so far?
I made a POST request to this endpoint:
https://firestore.googleapis.com/v1/projects/fuze-eee88/databases/(default)/documents/2020/abhishek/friend-requests/?documentId=newdocument
Request body:
{
"fields": {
"status": {
"booleanValue":"true"
}
}
}
The error I get:
{
"error": {
"code": 400,
"message": "Document parent name \"projects/fuze-eee88/databases/(default)/documents/2020//abhishek\" lacks a resource id at index 55.",
"status": "INVALID_ARGUMENT"
}
}

Seems you are getting that error because the document with the ID newdocument already exists in your Firestore database.You have to choose a unique ID for every new document that you add to Firestore database. You may try using a different ID, such as newdocument1 instead of new document.
You can have a look at below sample url
https://firestore.googleapis.com/v1/projects/<YOUR_PROJECT_ID>/databases/(default)/documents/<YOUR_COLLECTION_NAME>/<YOUR_DOCUMENT_ID>/<YOUR_SUBCOLLECTION_NAME>/?documentId=<YOUR_UNIQUE_ID>
After editing the above url as per your configuration will become:
https://firestore.googleapis.com/v1/projects/fuze-eee88/databases/(default)/documents/2020/abhishek/friend-requests/?documentId=newdocument1

Related

How to generate automatic Id with Commit or Batch Document Firestore REST

Hi I am creating documents with commit like this way:
{
"writes": [
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/?documentId=",
"fields": {
"comment": {
"stringValue": "Hello World!"
}
}
}
},
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/?documentId=",
"fields": {
"comment": {
"stringValue": "Happy Birthday!"
}
}
}
}
]
}
The parameter ?documentId= dosen´t work like when creating a single document, if I left empty I get an error that I must specify the name of the document so how I can generate an automatic id for each document?
Unfortunately, batch commits with auto generated documentId are not possible in the Firestore REST API. As you can see in this documentation, the Document object should be provided with a full path, including the documentID:
“Name:string
The resource name of the document, for example projects/{project_id}/databases/{databaseId}/documents/{document_path}.”
And if it was possible to omit the documentID, it would be mentioned in this documentation.
If you would like to have this implemented in the Firestore REST API, you can create a feature request in Google’s Issue Tracker so that they can consider implementing it.
I just came across the same problem and discovered that it is still not implemented.
I created a feature request for it here: https://issuetracker.google.com/issues/227875470.
So please go star it if you want this to be added.

Query Firestore Rest API by multiple document ids

I have noticed that documents that are returned by the API / runQuery do contain the document name, but I can't find any information within the structuredQuery docs how to query by multiple ids.
{
document: {
name: 'projects/:projectId/databases/(default)/documents/collection/id',
fields: ...,
createTime: '2021-01-16T13:35:02.151442Z',
updateTime: '2021-01-18T10:42:32.257199Z'
},
readTime: '2021-01-18T17:10:33.600112Z'
},
Firestore does support a whereIn query by document id Collection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"]), so I thought the Rest API might support is as well.
Do I miss something, or does runQuery not support queryById functionality?
This can be done by calling batchGet endpoint:
https://content-firestore.googleapis.com/v1beta1/projects/<PROJECT_ID>/databases/(default)/documents:batchGet
with the following JSON on the request body.
{
"documents": [
"projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID1>",
"projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID2>",
"projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID3>",
]
}

CloudKit Bad Request on Users Record Update with Web Services API

I added a custom field to the default Users record type in CloudKit, and I'm trying to push a value to that new field.
Here's how my request is structured in Node JS:
var query = {
operations :[{
operationType: 'forceUpdate',
record:{
recordType: 'Users',
fields:{
myCustomField: { value: 'stuff' }
},
recordName: '_abc123'
}
}]
}
I'm getting this response from CloudKit:
records: [{
recordName: '_abc123',
reason: 'invalid id string, id=_abc123',
serverErrorCode: 'BAD_REQUEST'
}]
If I put that same custom field on another, custom Record Type (like if I make my own User (without the "s") type) that also has myCustomField on it, the update works fine. So there must be something special I have to do to update the system Users type.
Does anyone know how to update a field on a Users record with the web services API?

How to pass owner attribute when creating user story using Rally API?

I am trying to pass the owner attribute to create a user story in rally using rally API But I am encountering below error.
{
"CreateResult": {
"_rallyAPIMajor": "2",
"_rallyAPIMinor": "0",
"Errors": [
"Cannot parse object reference from \"{\"Owner\": {\"_refObjectName\": \"Ron\"}}\""
],
"Warnings": [
"Ignored JSON element HierarchicalRequirement.PortfolioItem during the processing of this request."
]
}
}
My request payload
{
"HierarchicalRequirement":{
"Name": "hello Wrold",
"Description":" 123 test description",
"Workspace": "/workspace/18686460234",
"Project":"/project/1025697468602323",
"PortfolioItem":"",
"Owner":{"_refObjectName":"Ron"},
"ScheduleState":"Defined"
}
}
Any thoughts?
In general, when referring to an object property that itself is an object (as in this case with the User object), you pass in the actual value of _ref, not another object. If you have previously been passed the reference to the user as a full blown URI, then you can still pass that in and the SDK will convert it to a _ref.
If you go to the webservice docs (https://rally1.rallydev.com/slm/doc/webservice/) for your subscription and go down to the User section, you can get the docs to fetch you some examples of users. The _ref will come back something like:
https://rally1.rallydev.com/slm/webservice/v2.0/user/39776836851
I believe that you can either use that, or just truncate it to the number at the end. So the code will need to be changed so that the Owner line reads:
"Owner" : "https://rally1.rallydev.com/slm/webservice/v2.0/user/39776836851"

JSON server - Is it possible to update an object id?

Here is my db.json :
{
"users": [
{
"id": "1"
"name": "John"
}
]
}
I'd like to be able to update the user id by sending a PUT request on the existing user. But the following does not work:
Request URL :
PUT /users/1
with body:
{
"id": "2"
"name": "John"
}
Is there a way to update an object id?
If you are using PUT request means ,the request URL should be like this "PUT/users/1" .
Refer below mentioned image.
I'm using postman to send put request
This does not seem to be possible, as said in documentation:
Id values are not mutable. Any id value in the body of your PUT or
PATCH request will be ignored. Only a value set in a POST request will
be respected, but only if not already taken.