简体   繁体   English

如何从 Firestore 获取 DocumentReference 字段?

[英]How do I get a DocumentReference field from Firestore?

Im trying to retrieve an object from a google Firestore, this is the object:我试图从谷歌 Firestore 检索一个对象,这是对象:

Firestore object Firestore 对象

Since i added the atribute soat im getting erros im using this class to deserialize the object:由于我添加了属性 soat 我得到了错误,我使用这个类来反序列化对象:

@Data
public class Car {
    private String plate;
    private String model;
    private long km;
    private boolean available;
    private DocumentReference soat;   
}

And this code to retrieve the all the Car objects from the Firestore这段代码从 Firestore 中检索所有 Car 对象

     @Override
    public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
                Car emp = doc.toObject(Car.class);
                empList.add(emp);
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
        return empList;
    }

Since i added the property soat im getting this error whe requesting the data由于我添加了属性 soat 我在请求数据时收到此错误

ERROR 7544 --- [nio-8080-exec-7] seErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [request route] and exception [Could not write JSON: Infinite recursion (StackOverflowError);错误 7544 --- [nio-8080-exec-7] seErrorMvcAutoConfiguration$StaticView:无法为请求 [请求路由] 和异常呈现错误页面 [无法写入 JSON:无限递归 (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options"]... ->com.google.cloud.firestore.FirestoreOptions["credentia ["modulus"])] as the response has already been committed. As a result,嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)(通过参考链:com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions[" service"]->com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options" ]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options"]...->com.google.cloud.firestore.FirestoreOptions["credentia ["modulus"])] 因为响应已经提交。因此,

Previously i couldnt make the request properly so i added this to my application.properties以前我无法正确发出请求,所以我将此添加到我的 application.properties

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

Edit: just tried this aproach and still getting same error编辑:刚刚尝试了这种方法,但仍然出现相同的错误

    @Override
    public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
               
                 String plate=doc.getData().get("plate").toString();
                 String model=doc.getData().get("model").toString();
                 long km=Long.parseLong(doc.getData().get("km").toString());
                 boolean available=Boolean.parseBoolean(doc.getData().get("available").toString());
                 DocumentReference soat=(DocumentReference)doc.getData().get("soat");

                Car emp = new Car();
                emp.setAvailable(available);
                emp.setKm(km);
                emp.setModel(model);
                emp.setPlate(plate);
                emp.setSoat(soat);

                empList.add(emp);
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
        return empList;
    }

-------EDIT-------- - - - -编辑 - - - -
I have aded this to my application.properties我已将此添加到我的 application.properties

spring.jackson.serialization.fail-on-self-references=false

And now i get this from the Firestore call现在我从 Firestore 电话中得到了这个

[{"plate":"HEO628","model":"2014","km":75000,"available":true,"soat":{"path":"SOATS/HEO628","parent":{"parent":null,"id":"SOATS","path":"SOATS","firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":

Basically you have to create a class to deserialice your DatabaseReference, in this case the class name would be SOAT and use it to sotre your reference to the database.基本上,您必须创建一个类来反序列化您的 DatabaseReference,在这种情况下,类名将是SOAT并使用它来存储您对数据库的引用。

@Data
public class Car {
    private String plate;
    private String model;
    private long km;
    private boolean available;
    private SOAT soat;  
    private Insurance insurance;
    private Techno techno;
}

Then when retrieving the information from FireBase you have to make the following requests in order to specify the kind of object your are going to map.然后,当从 FireBase 检索信息时,您必须发出以下请求以指定要映射的对象类型。

public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {

                String plate = doc.getData().get("plate").toString();
                String model = doc.getData().get("model").toString();
                long km = Long.parseLong(doc.getData().get("km").toString());
                boolean available = Boolean.parseBoolean(doc.getData().get("available").toString());
                DocumentSnapshot soatref = fb.getFirestore().collection("SOATS").document(plate).get().get();
                DocumentSnapshot technoref = fb.getFirestore().collection("Technos").document(plate).get().get();
                DocumentSnapshot insuranceref = fb.getFirestore().collection("Insurances").document(plate).get().get();
                SOAT soat = soatref.toObject(SOAT.class);
                Techno techno = technoref.toObject(Techno.class);
                Insurance insurance = insuranceref.toObject(Insurance.class);

                Car emp = new Car();
                emp.setAvailable(available);
                emp.setKm(km);
                emp.setModel(model);
                emp.setPlate(plate);
                emp.setSoat(soat);
                emp.setInsurance(insurance);
                emp.setTechno(techno);

                empList.add(emp);
            }
        } catch (Exception e) {

        }
        return empList;
    }

An then whe calling your API you shold get a proper reference to the Object like this.然后当你调用你的 API 时,你会像这样得到一个对对象的正确引用。

{
    "plate": "HEO628",
    "model": "2014",
    "km": 75000,
    "available": true,
    "soat": {
      "expeditionDate": "2020-09-29T06:12:12.000+00:00",
      "carPlate": "HEO628"
    },
    "insurance": {
      "expeditionDate": "2020-10-01T11:12:12.000+00:00",
      "carPlate": "HEO628"
    },
    "techno": {
      "expeditionDate": "2020-09-08T17:00:00.000+00:00",
      "carPlate": "HEO628"
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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