Dranreb Benedicto
Dranreb Benedicto

Reputation: 89

FireStore Keeps Replacing Field - Android

I have an activity in which after the user has created their account (Authentication), it would then automatically place a complete_profile=true in the user's document in FireStore. To give you more perspective, here is what my firestore looks like:

users <-- Collection
  |
  A8as7f9aKsrYas12l7 <--- User's ID/Document
         |
         complete_profile: "true" <-- Field

Now, I have another page called completeprofileactivity. It asks for the user's name. after that, I'd like to add another field in my user's data name: "sample name". Problem is that after adding the name, the complete_profile vanished:

users 
  |
  A8as7f9aKsrYas12l7 
         |
         name: "sample name" <-- replaced?

When I go and recreate a new account again with the same user, it should add the complete_profile right? So I'll have both name and complete_profile. But instead I got this again:

users 
  |
  A8as7f9aKsrYas12l7 
         |
         complete_profile: "true" <-- it replaced the "name" again

I have this code for my Complete Profile:

Boolean userdoComplete = true;
String str_userid = mAuth.getCurrentUser().getUid();

Map<String, String> user_map = new HashMap<>();
    user_map.put("complete_profile",userdoComplete.toString());

    mFirestore.collection("users").document(str_userid).set(user_map).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if(task.isSuccessful()){
                                    //Registered Successfully
                                    //Intent to Main Activity
                                    Intent goMain = new Intent(SignUpActivity.this, MainActivity.class);
                                    startActivity(goMain);

                                } else {

                                }
                            }
                        });

And for adding the Name:

String str_userid = mAuth.getCurrentUser().getUid();
Map<String, String> specialMap = new HashMap<>();
        specialMap.put("name", name);
        mFireStore.collection("users").document(str_userid).set(specialMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    //All is Successful - Proceed
                    Toast.makeText(AddSpecialActivity.this, "Successful!", Toast.LENGTH_SHORT).show();

                } else {
                    //FireStore Error
                    Toast.makeText(AddSpecialActivity.this, "There was an Error. Please Try Again.", Toast.LENGTH_LONG).show();
                }
            }
        });

I don't get it why one keeps replacing the other. It should add both complete_profile and name.

Upvotes: 0

Views: 755

Answers (2)

Todd Kerpelman
Todd Kerpelman

Reputation: 17523

set replaces the entire document. You should consider using the update call instead to update or add a field to an existing document.

https://firebase.google.com/docs/firestore/manage-data/add-data#update-data

Upvotes: 1

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126814

As you have noticed, this code:

mFireStore.collection("users").document(str_userid).set(specialMap)

The snippet replaces everything within the document referenced with str_userid. This is of course the intended behavior because you set the document to your specialMap. Their is an easy fix to this, which is to use the merge() SetOption.

mFireStore.collection("users").document(str_userid).set(specialMap, SetOptions.merge())

For more information you can check out the Firestore documentation here.

Upvotes: 1

Related Questions