2
votes

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())
2

2 Answers

4
votes

try this

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

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);