gsposato
gsposato

Reputation: 107

Getting An Error when trying to create a subcollection in Firestore

I am trying to create a node in Google Firebase, and use its unique id to create a Document in Google Firestore of the same name.

I'm using Google's PHP Firestore Client: https://github.com/GoogleCloudPlatform/google-cloud-php-firestore

And I've read through their documentation: http://googlecloudplatform.github.io/google-cloud-php/#/docs/cloud-firestore/v0.5.1/firestore/writebatch

Here is my code:

<?php

use \Google\Cloud\Firestore\FirestoreClient;
use \Google\Cloud\Core\Timestamp;
use \Google\Cloud\Firestore\Transaction as FirestoreTransaction;
use \grptx\Firebase as FirebaseClient;

class FirestoreTest
{
    public function create()
    {
        $client = new FirebaseClient();
        $database = $client->getDatabase();

        $org = array(
            "acl" => array(),
            "people" => array()
        );

        $ref = $database->getReference("/clients/")->push($org);
        $key = $ref->getKey();

        $config = array(
            "projectId" => "xxx",
            "keyFile" => json_decode(file_get_contents("/xxx/firebase_auth.json"), true)
        );

        $firestore = new FirestoreClient($config);
        $batch = $firestore->batch();
        $collection = $firestore->collection("clients")->document("-LXXXXXX")->collection("trips");
    }
}

And I get this error:

Exception 'Google\Cloud\Core\Exception\BadRequestException' with message '{
"message": "Document name \"projects\/xxx-test\/databases\/(default)\/documents\/clients\/\" has invalid trailing \"\/\".",
"code": 3,
"status": "INVALID_ARGUMENT",
"details": []
}'

Any help is appreciated.

Upvotes: 3

Views: 3890

Answers (2)

str028
str028

Reputation: 204

Basically this will happen if you try to put blank as document name.

Upvotes: 5

AndersonHappens
AndersonHappens

Reputation: 547

This is the error that occurs if you try to get a collection as a document. It's kind of tricky because this can also happen if you try to get a document with the name of empty string in a collection.

I don't know PHP, but I would guess that either in your $database->getReference("/clients/")->push($org); call, you were supposed to name a document to push your information to, or in your $firestore->collection("clients")->document("-LXXXXXX")->collection("trips"); call that the document you are trying to get ("-LXXXXXX") has the name empty string. (Of course, this is assuming your document isn't actually named "-LXXXXXX", and you are using that as a substitute for some variable that happens to be equal to "").

For instance, in python this call randomly failed me earlier:

db.collection(u'data').document(current_id)

with the same error: 'Document name ".../documents/data/" has invalid trailing "/". and will exit.' I scratched my head for a while but that's because the variable current_id is the empty string.

Basically, internally Firebase converts it into a long pathname and then tries to get a document or a collection at that pathname depending on what your last call was. This causes an issue if you try to get a document that is named "".

Upvotes: 1

Related Questions