【发布时间】: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