Invalid document reference. Document references must have an even number of segments










0















I am querying a collection for documents using below code. Document names are same as device names. The device name is passed to the document from an API I am getting an error when a device name contains letter "/" like Motorola C380/C385.
I get an error




java.lang.IllegalArgumentException: Invalid document reference.
Document references must have an even number of segments, but
Mobiles/Motorola C380/C385 has 3




I know I am getting this error because FireStore considers the name C385 after slash as a collection inside a document. I want to know how to get rid of situations like this when a document name can contain a "/" should I check and remove this before inserting it into my collection or is there any better solution?



firebaseFirestore.collection("Mobiles").document(response.body().get(finalI).getDeviceName())









share|improve this question




























    0















    I am querying a collection for documents using below code. Document names are same as device names. The device name is passed to the document from an API I am getting an error when a device name contains letter "/" like Motorola C380/C385.
    I get an error




    java.lang.IllegalArgumentException: Invalid document reference.
    Document references must have an even number of segments, but
    Mobiles/Motorola C380/C385 has 3




    I know I am getting this error because FireStore considers the name C385 after slash as a collection inside a document. I want to know how to get rid of situations like this when a document name can contain a "/" should I check and remove this before inserting it into my collection or is there any better solution?



    firebaseFirestore.collection("Mobiles").document(response.body().get(finalI).getDeviceName())









    share|improve this question


























      0












      0








      0








      I am querying a collection for documents using below code. Document names are same as device names. The device name is passed to the document from an API I am getting an error when a device name contains letter "/" like Motorola C380/C385.
      I get an error




      java.lang.IllegalArgumentException: Invalid document reference.
      Document references must have an even number of segments, but
      Mobiles/Motorola C380/C385 has 3




      I know I am getting this error because FireStore considers the name C385 after slash as a collection inside a document. I want to know how to get rid of situations like this when a document name can contain a "/" should I check and remove this before inserting it into my collection or is there any better solution?



      firebaseFirestore.collection("Mobiles").document(response.body().get(finalI).getDeviceName())









      share|improve this question
















      I am querying a collection for documents using below code. Document names are same as device names. The device name is passed to the document from an API I am getting an error when a device name contains letter "/" like Motorola C380/C385.
      I get an error




      java.lang.IllegalArgumentException: Invalid document reference.
      Document references must have an even number of segments, but
      Mobiles/Motorola C380/C385 has 3




      I know I am getting this error because FireStore considers the name C385 after slash as a collection inside a document. I want to know how to get rid of situations like this when a document name can contain a "/" should I check and remove this before inserting it into my collection or is there any better solution?



      firebaseFirestore.collection("Mobiles").document(response.body().get(finalI).getDeviceName())






      java android firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 11:28







      walter

















      asked Nov 14 '18 at 11:16









      walterwalter

      96




      96






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The simplest method I can think of, is before you add the data to the database to find that forbidden / symbol and replace it with an allowed one, let's say - (minus).



          String deviceName = response.body().get(finalI).getDeviceName().replaceAll("/", "-");


          And then simply use in your reference like this:



          firebaseFirestore.collection("Mobiles").document(deviceName);





          share|improve this answer























          • Hi walter! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 15 '18 at 7:43


















          1














          try this



          response.body().get(finalI).getDeviceName().toString().replace("/","_")





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298957%2finvalid-document-reference-document-references-must-have-an-even-number-of-segm%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The simplest method I can think of, is before you add the data to the database to find that forbidden / symbol and replace it with an allowed one, let's say - (minus).



            String deviceName = response.body().get(finalI).getDeviceName().replaceAll("/", "-");


            And then simply use in your reference like this:



            firebaseFirestore.collection("Mobiles").document(deviceName);





            share|improve this answer























            • Hi walter! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 15 '18 at 7:43















            1














            The simplest method I can think of, is before you add the data to the database to find that forbidden / symbol and replace it with an allowed one, let's say - (minus).



            String deviceName = response.body().get(finalI).getDeviceName().replaceAll("/", "-");


            And then simply use in your reference like this:



            firebaseFirestore.collection("Mobiles").document(deviceName);





            share|improve this answer























            • Hi walter! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 15 '18 at 7:43













            1












            1








            1







            The simplest method I can think of, is before you add the data to the database to find that forbidden / symbol and replace it with an allowed one, let's say - (minus).



            String deviceName = response.body().get(finalI).getDeviceName().replaceAll("/", "-");


            And then simply use in your reference like this:



            firebaseFirestore.collection("Mobiles").document(deviceName);





            share|improve this answer













            The simplest method I can think of, is before you add the data to the database to find that forbidden / symbol and replace it with an allowed one, let's say - (minus).



            String deviceName = response.body().get(finalI).getDeviceName().replaceAll("/", "-");


            And then simply use in your reference like this:



            firebaseFirestore.collection("Mobiles").document(deviceName);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 11:36









            Alex MamoAlex Mamo

            43.3k82860




            43.3k82860












            • Hi walter! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 15 '18 at 7:43

















            • Hi walter! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 15 '18 at 7:43
















            Hi walter! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 15 '18 at 7:43





            Hi walter! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 15 '18 at 7:43













            1














            try this



            response.body().get(finalI).getDeviceName().toString().replace("/","_")





            share|improve this answer





























              1














              try this



              response.body().get(finalI).getDeviceName().toString().replace("/","_")





              share|improve this answer



























                1












                1








                1







                try this



                response.body().get(finalI).getDeviceName().toString().replace("/","_")





                share|improve this answer















                try this



                response.body().get(finalI).getDeviceName().toString().replace("/","_")






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 14:25









                Frank van Puffelen

                236k29381407




                236k29381407










                answered Nov 14 '18 at 11:33









                Saravana kumarSaravana kumar

                213




                213



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298957%2finvalid-document-reference-document-references-must-have-an-even-number-of-segm%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto