【问题标题】:How can I retreive a custom object from Firestore which references another custom object?如何从 Firestore 中检索引用另一个自定义对象的自定义对象?
【发布时间】:2021-06-21 23:51:42
【问题描述】:

我有一个 User 类和一个 Pet 类。一个用户可以拥有多个宠物。 我正在尝试检索用户文档并将其转换为用户对象,如下所示:

loggedInUser = documentSnapshot.toObject(User.class);

它会抛出以下异常:

java.lang.RuntimeException:无法反序列化对象。无法将 com.google.firebase.firestore.DocumentReference 类型的对象转换为 com.example.pawsibilities.Pet 类型(在字段“pets.[0]”中找到)

Here 是 firestore 中的用户示例。其中一个字段是引用数组(宠物)。

我的宠物类在 Firestore 中看起来像 this,在 Java 中看起来像这样:

public class Pet {
    private String name;
    private String type;

    private LocalDate birthday;
    private String breed;

    public enum Gender {
        Female,
        Male,
        Unknown
    }
    private Gender gender;
    private boolean neutered;

    private float height;
    private float weight;
    private String healthNotes;

    private boolean lostStatus = false;
    private LocalDate lostSince = null;
    private ArrayList<LastSeenDetails> lastSeenDetailsList = null;

    public Pet() {    }

    public Pet(String name, String type, LocalDate birthday, String breed, Gender gender, Boolean neutered, float height, float weight, String healthNotes) {
        this.name = name;
        this.type = type;
        this.birthday = birthday;
        this.breed = breed;
        this.gender = gender;
        this.neutered = neutered;
        this.height = height;
        this.weight = weight;
        this.healthNotes = healthNotes;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getBirthday() {
        return birthday.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
    }

    public String getLostSince() {
        return lostSince.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
    }

    public String getBreed() {
        return breed;
    }

    public String getGender() {
        return gender.name();
    }

    public String getHeight() {
        return height + " cm";
    }

    public String getWeight() {
        return weight + " kg";
    }

    public String getHealthNotes() {
        return healthNotes;
    }

    public ArrayList<LastSeenDetails> getLastSeenDetailsList() {
        return lastSeenDetailsList;
    }

    public boolean isLost(){ return lostStatus; }

    public String isNeutered() {
        if (neutered == true) {
            return "Yes";
        } else
            return "No";
    }

    public void setLostStatus(boolean lostStatus) {
        this.lostStatus = lostStatus;
    }

    public void setLostSince(LocalDate time) { this.lostSince = time; }

    public void setLastSeenDetailsList(ArrayList<LastSeenDetails> lastSeenDetailsList) {
        this.lastSeenDetailsList = lastSeenDetailsList;
    }
}

它有一个空的构造函数,并且所有字段都有一个 getter 方法。我似乎找不到问题...

【问题讨论】:

    标签: java google-cloud-firestore


    【解决方案1】:

    不需要将文档转换为用户,因为检索到的文档是用户类型的对象。

    试试这个,

    User user = documentSnapshot.getData();
    

    User user = documentSnapshot.get(String field);
    

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多