简体   繁体   中英

How can I retreive a custom object from Firestore which references another custom object?

I have a User class and a Pet class. A user can have multiple pets. I'm trying to retreive a user document and convert it to a user object as follows:

loggedInUser = documentSnapshot.toObject(User.class);

It throws the following exception:

java.lang.RuntimeException: Could not deserialize object. Can't convert object of type com.google.firebase.firestore.DocumentReference to type com.example.pawsibilities.Pet (found in field 'pets.[0]')

Here is an example of a user in firestore. One of its fields is an array of references (pets).

My pet class look like this in Firestore, and it looks like this in 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;
    }
}

It has an empty constructor and all fields have a getter method. I can't seem to find the issue...

It's not necessary to convert document to User, as the retrieved document is an object of type User.

Try this,

User user = documentSnapshot.getData();

or

User user = documentSnapshot.get(String field);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM