Home > Software engineering >  Java Firestore - Error: Use FieldPath.of() for field names containing '˜*/[]'
Java Firestore - Error: Use FieldPath.of() for field names containing '˜*/[]'

Time:04-22

I am writing Java code to interact with a Firestore database. I need to access a field containing a forward-slash in the field name:

f.col1 = document.getString("username");
f.col2 = document.getString("subject");
f.col3 = document.getString("details/comments");

When I run the code, I get the following error for the line getting Col3:

java.lang.IllegalArgumentException: Use FieldPath.of() for field names containing '˜*/[]'.

I am unable to work out how to correctly use this method and I cannot find any documentation on how I should correctly use it (Googling brings up an equivalent JS method). When I attempt to use the FieldPath.of() method, as follows:

f.col3 = document.getString(FieldPath.of("details/comments"));

I get the following compiler error:

java: incompatible types: com.google.cloud.firestore.FieldPath cannot be converted to java.lang.String

I have no control over the structure of the Firebase data, so I need to work with this field name.

I am using the following documentation to interact with the database: https://firebase.google.com/docs/firestore/quickstart#java_9

CodePudding user response:

I think you're looking for FieldPath.of for that last field:

f.col3 = document.getString(FieldPath.of("details/comments"));

If the / was meant to indicate the comments is a subfield inside the details map, the correct separator would be a . btw:

f.col3 = document.getString(FieldPath.of("details.comments"));

For your second error, it looks like DocumentSnapshot.getString() only exists with a String parameter, so you'll want to use get() which accepts a FieldPath:

f.col3 = document.get(FieldPath.of("details/comments"));

or

f.col3 = document.get(FieldPath.of("details.comments"));

If you're getting a string conversion error here, add toString() to the call.

  • Related