Search code examples
androidgoogle-cloud-firestorelogic

Way to Add data in Firestore


I am creating a Java App, and using firestore as the database, I intent to add data to the Firestore when user click on "Join Btn". All variable is already defined in model class. The problem now is when I click the Join Btn, the data is not stored in Firestore. I am not sure why does this happen.

Below shows the model class and addedToJoined function.

private String eventName;
private String eventDate;
private String eventVenue;
private String eventDesc;
private String eventDocId;

private void addedToJoined(){

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

    joinedMap.put("eventName", event.getEventName());
    joinedMap.put("eventDate", event.getEventDate());
    joinedMap.put("eventVenue", event.getEventVenue());
    joinedMap.put("eventDesc", event.getEventDesc());

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

    participant.put("eventName", event.getEventName());
    participant.put("participated_users_name", fAuth.getCurrentUser().getDisplayName());



    fStore.collection("CurrentUser").document(fAuth.getCurrentUser().getUid())
            .collection("MyFavourite").add(joinedMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
        @Override
        public void onComplete(@NonNull @NotNull Task<DocumentReference> task) {
            Toast.makeText(EventDetail.this, "Event saved.", Toast.LENGTH_SHORT).show();
            finish();
        }
    });

    fStore.collection("EventDetail").document(event.getEventDocId()).collection("event").add(participant).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
        @Override
        public void onComplete(@NonNull @NotNull Task<DocumentReference> task) {
            Toast.makeText(EventDetail.this, "Event added to record.", Toast.LENGTH_SHORT).show();
            finish();
        }
    });
}

Solution

  • Try implementing the code with the help of public documentation in which the whole description of storing data into Firestore has been provided with the help of onSuccess and onFailure listeners and the video in which it has been explained along with using the onComplete Listener at 06:36.if the problem persist kindly share any error message you are getting.