Android(Firestore):无法在Firestore中已创建的文档中添加、更新或设置(SetOptions.merge)字段

Android(Firestore):无法在Firestore中已创建的文档中添加、更新或设置(SetOptions.merge)字段,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我正在尝试向firestore数据库中“users”集合下已创建的文档添加新字段。 从第一个活动开始,我向firestore数据库(集合(“用户”).document(uid))添加了基本信息,如姓名、描述和性别,并且工作正常。但是,当我尝试向同一文档添加新字段(来自第二个活动)时,它不起作用。我尝试使用set,SetOptions.merge,update,但没有任何效果。即使我试图添加新的收集,然后也没有工作。有人能帮我弄清楚这件事吗。提前谢谢。 我的代码如下所示 第一个活动(BasicIn

我正在尝试向firestore数据库中“users”集合下已创建的文档添加新字段。 从第一个活动开始,我向firestore数据库(集合(“用户”).document(uid))添加了基本信息,如姓名、描述和性别,并且工作正常。但是,当我尝试向同一文档添加新字段(来自第二个活动)时,它不起作用。我尝试使用set,SetOptions.merge,update,但没有任何效果。即使我试图添加新的收集,然后也没有工作。有人能帮我弄清楚这件事吗。提前谢谢。 我的代码如下所示

第一个活动(BasicInfo.java)(工作)

String uid=user.getUid();
Map dataToSave=new HashMap();
dataToSave.put(NAME,nameText);
数据保存。放置(大约,大约文本);
dataToSave.put(性别、用户性别);
db.collection(“users”).document(uid).set(dataToSave).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Intent userPreIntent=newintent(BasicInfo.this,UserPre.class);
startActivity(userPreIntent);
完成();
}否则{
Toast.makeText(getApplicationContext(),“出现了一些问题”,Toast.LENGTH\u SHORT.show();
}
}
});
第二个活动(UserPre.java)(不工作)

Map data=newhashmap();
data.put(STYLE,styleBeauty);
(幽默,幽默);
数据。放置(适合度,适合度);
数据输入(行程、行程);
数据。put(摄影、摄影);
数据输入(音乐,音乐);
数据。放置(舞蹈、舞蹈);
数据输入(艺术,艺术);
data.put(时尚,时尚);
字符串uid=user.getUid();
db.collection(“user”).document(uid).set(data).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(getApplicationContext(),“添加数据”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“出现了一些问题”,Toast.LENGTH\u SHORT.show();
}
}
});

您可以添加FailureListener,它将告诉您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);
    }
});
db.collection(“user”).document(uid).set(data).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(getApplicationContext(),“添加数据”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“出现了一些问题”,Toast.LENGTH\u SHORT.show();
}
}
})
.addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Log.w(标签“错误写入文件”,e);
}
});

风格、幽默、健康等等都是字符串吗?您是否也尝试过使用
db.collection(“user”).document(uid).set(data,SetOptions.merge()).addOnCompleteListener()
?这是什么行为?@AlexMamo是的,这些是字符串,是的,我确实使用了set(data,SetOptions.merge(),但问题通过在数据库规则文件中进行更改得到了解决。出现了一些权限问题。现在使用set(data,SetOptions.merge())可以正常工作。谢谢,出现权限问题。通过更改数据库中“规则”选项卡中的规则解决了此问题。很高兴能提供帮助,请接受答案以供将来参考。
    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();
            }
        }
    });
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);
    }
});