0

How should I fetch the document fields from one collection and combine them to add a new document to another collection? I have attached picture of the database how does it looks, I want to fetch the fields from the collection show and want to update it to the new collection along with some other data:

private void savePost(String mPostTitle, String mPostContent, String mlistSpinnerC) {
        final DocumentReference docRef = FirebaseFirestore.getInstance().collection("users").document(mauth.getCurrentUser().getUid());
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        String username = (String) 
document.get("username");
String email= (String) document.get(email);
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });

        postMap.put(Constants.POSTTTITLE, mPostTitle);
        postMap.put(Constants.POSTCATEGORY, mlistSpinnerC);
        postMap.put(Constants.POSTCONTENT, mPostContent);
        postMap.put(Constants.TIMESTAMP, (System.currentTimeMillis()/1000));
        postMap.put(Constants.USER_ID,mauth.getCurrentUser().getUid());
        postMap.put("username", username);


        PostsRef.document().set(postMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                if(task.isSuccessful()){
                    Intent toHomeActivity = new Intent(AddPostActivity.this, MainActivity.class);
                    startActivity(toHomeActivity);
                }

            }
        });

I am just not able to map the fields from one collection to another collection, please guide me the correct method to that.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133

1 Answers1

0

By the time you are trying to add the username to your postMap using the following line of code:

postMap.put("username", username);

The data has not finished loading yet from the database and this is because the listener you have added to your get() call is being invoked some unknown amount of time later after your query finishes. You don't know how long it's going to take, it may take from a few hundred milliseconds to a few seconds before that data is available. The onComplete() method has an asynchronous behavior, that's why you cannot get that username in such a way.

A quick solve for this problem would be to move all that block of code related to adding data to the postMap, inside the onComplete() method. In this you are waiting for the callback and username your will be available. Otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Can I use firestore transactions in this case? if so, guide me for that as well. Thank You –  Aug 12 '19 at 05:44
  • You should use transactions only when you need to know a value before you change it. Please also note that a transaction absolutely requires round trip communications with server in order to ensure that the code inside the transaction completes successfully. So in your case, to simply read a document, there is no need to use transactions. – Alex Mamo Aug 12 '19 at 10:14