How to assign field value of a collection document to another collection document field as a value in Firestore using java? [closed]

為{幸葍}努か 提交于 2021-02-05 11:24:27

问题


I have 2 collections; BookingInformation and ParkingArea. From the ParkingArea document, I want to save the latest value of the "areanumber" field to the BookingInformation collection with the field name "Area".

I have tried the following. In logcat, I can view the required data but no clue how to assign it. I didn't find any resources. Please help.

final List<String> parkingDocList = new ArrayList<>();

dataStore.collection("ParkingArea").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                parkingDocList.add(documentSnapshot.getId());
            }
        } 
    }
});

final Map<String, Object> reserveInfo = new HashMap<>();

reserveInfo.put("date", date.getText().toString());
reserveInfo.put("startTime", startTime.getText().toString());
reserveInfo.put("endTime", endTime.getText().toString());
reserveInfo.put("userId", currentUserId);
reserveInfo.put("area", allocatedArea[0]);

dataStore.collection("BookingInformation").document(id).set(reserveInfo)
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Toast toast4 =
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Area reserved successfully", Toast.LENGTH_SHORT);
            toast4.show();
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast toast5 = Toast.makeText(getActivity().getApplicationContext(), "Reservation failed", Toast.LENGTH_SHORT);
            toast5.show();
        }
    });

回答1:


Data is loaded from Firestore (and most cloud APIs) asynchronously. This means that any code that needs the data from the database, must be inside the onComplete handler that is called when the data is loaded.

So in your case:

final List<String> parkingDocList = new ArrayList<>();

dataStore.collection("ParkingArea").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                parkingDocList.add(documentSnapshot.getId());
            }

            final Map<String, Object> reserveInfo = new HashMap<>();

            reserveInfo.put("date", date.getText().toString());
            reserveInfo.put("startTime", startTime.getText().toString());
            reserveInfo.put("endTime", endTime.getText().toString());
            reserveInfo.put("userId", currentUserId);
            reserveInfo.put("area", allocatedArea[0]);

            dataStore.collection("BookingInformation").document(id).set(reserveInfo)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast toast4 =
                                Toast.makeText(getActivity().getApplicationContext(),
                                        "Area reserved successfully", Toast.LENGTH_SHORT);
                        toast4.show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast toast5 = Toast.makeText(getActivity().getApplicationContext(), "Reservation failed", Toast.LENGTH_SHORT);
                        toast5.show();
                    }
                });
        } 
    }
});


来源:https://stackoverflow.com/questions/61542953/how-to-assign-field-value-of-a-collection-document-to-another-collection-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!