问题
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