Ad

How Should I Fetch The Document Fields And Use Them In Another Map For Another Collection?

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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad