Sandeep Pradhan
Sandeep Pradhan

Reputation: 15

Android (Firestore): Unable to add or update or set (SetOptions.merge) fields in already created document in Firestore

I am trying to add new fields to an already created document under "users" collection in firestore database. From first activity I added basic information like name and description and gender to the firestore database (collection("users").document(uid)) and it worked. But when I am trying to add new fields (from second activity) to the same document it is not working. I tried to use set, SetOptions.merge, update, but nothing is working. Even I tried to add new collection then also it is not working. Can someone please help me to figure this out. Thanks in advance. My codes are below from both the activities.

First Activity (BasicInfo.java) (working)

    String uid = user.getUid();

    Map<String, Object> dataToSave = new HashMap<>();
    dataToSave.put(NAME, nameText);
    dataToSave.put(ABOUT, aboutText);
    dataToSave.put(GENDER, userGender);

    db.collection("users").document(uid).set(dataToSave).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Intent userPreIntent = new Intent(BasicInfo.this, UserPre.class);
                startActivity(userPreIntent);
                finish();
            } else {
                Toast.makeText(getApplicationContext(), "There was some problem", Toast.LENGTH_SHORT).show();
            }
        }
    });

Second Activity (UserPre.java) (Not working)

    Map<String, Object> data = new HashMap<>();
    data.put(STYLE, styleBeauty);
    data.put(HUMOR, humor);
    data.put(FITNESS, fitness);
    data.put(TRAVEL, travel);
    data.put(PHOTOGRAPHY, photography);
    data.put(MUSIC, music);
    data.put(DANCE, dance);
    data.put(ART, art);
    data.put(FASHION, fashion);

    String uid = user.getUid();

    db.collection("user").document(uid).set(data).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Data Added", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "There was some problem", Toast.LENGTH_SHORT).show();
            }
        }
    });

Upvotes: 1

Views: 973

Answers (1)

Sander
Sander

Reputation: 816

You can add a FailureListener and it will tell you what's wrong in logcat.

db.collection("user").document(uid).set(data).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Toast.makeText(getApplicationContext(), "Data Added", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "There was some problem", Toast.LENGTH_SHORT).show();
        }
    }
})
.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.w(TAG, "Error writing document", e);
    }
});

Upvotes: 1

Related Questions