Firestore无法反序列化对象。将类型为java.util.HashMap的值转换为Date失败。

huangapple go评论67阅读模式
英文:

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

问题

以下是翻译好的内容:

我正在尝试从Firestore集合中的第一个文档中读取一个字段,但是我遇到了以下错误:

> 无法反序列化对象。无法将类型为java.util.HashMap的值转换为Date类型

我有一个类似这样的Firestore数据库:

“User Routes”集合包含以每个用户ID命名的文档。在这些文档内部有多个**“Route”集合**。

每个**“Route”集合包含存储有3个字段的用户位置对象多个文档**。最终,我想要根据指定的日期(使用时间戳字段)筛选路线,并能够使用所有geo_point字段构建Polyline:

这是UserLocation对象(仅与时间戳相关的代码):

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

我正在尝试使用以下代码从第一个文档中读取时间戳字段:

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

这是Android Studio的屏幕截图,以便更容易查看:

我想要获取时间戳值,以便能够根据用户选择的日期范围对其进行筛选,但我无法访问它。

有什么想法吗?

英文:

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.

Firestore无法反序列化对象。将类型为java.util.HashMap的值转换为Date失败。

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:

Firestore无法反序列化对象。将类型为java.util.HashMap的值转换为Date失败。

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

Here's a screenshot of Android Studio for ease of view:
Firestore无法反序列化对象。将类型为java.util.HashMap的值转换为Date失败。

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?

答案1

得分: 1

如果您正在使用UserLocation来编写文档,那么您也会希使用该类来读取这些文档:

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

然后,您将遍历此列表,查找与查询相匹配的每个文档的日期。

Firestore不允许您从文档查询中提取单独的字段,您只能将整个文档作为结果获取。

英文:

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

List&lt;UserLocation&gt; 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.

huangapple
  • 本文由 发表于 2020年10月21日 04:43:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64453063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定