Skip to content
Advertisement

Firestore Could not deserialize object. Failed to convert value of type java.util.HashMap to Date

I’m trying to read a field from the first document in a Firestore Collection but I get the following error:

Could not deserialize object. Failed to convert value of type java.util.HashMap to Date

I have a Firestore Database that looks like this:

The “User Routes” collection contains documents named after each User’s ID. Inside those documents are multiple “Route” collections.

enter image description here

Each “Route” collection contains several documents storing a User Location Object with 3 fields. Ultimately, I want to filter the routes based on a specified date (using the timestamp field), and be able to build a Polyline using all of the geo_point fields:

enter image description here

This is the UserLocation Object (only code relevant to timestamp):

public class UserLocation {

    private GeoPoint geo_point;
    private @ServerTimestamp Date timestamp;
    private User user;

public Date getTimestamp() {
        return timestamp;
    }

public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

}

I’m trying to read the timestamp field from the first document using the following code:

Query query = mDb.collection("User Routes").document(user_id).collection("Route "+ i)
        .orderBy("timestamp", Query.Direction.ASCENDING)
        .limit(1);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()){
            routeDate = task.getResult().toObjects(Date.class);
            Log.d("Route Fragment", "onComplete: query "+routeDate);
        }
    }
});

Here’s a screenshot of Android Studio for ease of view: enter image description here

I want to get the timestamp value to be able to filter it against the user’s selected date range, but I can’t access it.

Any ideas?

Advertisement

Answer

If you are using UserLocation to write documents, then you would also want to use that class to read them:

List<UserLocation> locations = task.getResult().toObjects(UserLocation.class);

You would then iterate the list to find the dates for each document that matched the query.

Firestore doesn’t let you pull individual fields from document queries. You only get entire documents as a result.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement