gpt4 book ai didi

Android (Firestore) : Unable to add or update or set (SetOptions. 合并) 已在 Firestore 中创建的文档中的字段

转载 作者:行者123 更新时间:2023-11-29 23:46:11 25 4
gpt4 key购买 nike

我正在尝试向 firestore 数据库中“用户”集合下已创建的文档添加新字段。从第一个 Activity 开始,我将名称、描述和性别等基本信息添加到 firestore 数据库 (collection("users").document(uid)) 并且它起作用了。但是,当我尝试将新字段(来自第二个 Activity )添加到同一文档时,它不起作用。我尝试使用 set、SetOptions.merge、update,但没有任何效果。即使我尝试添加新集合,它也无法正常工作。有人可以帮我解决这个问题吗?提前致谢。我的代码在这两个 Activity 中。

第一个 Activity (BasicInfo.java)(工作)

    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();
}
}
});

第二个 Activity (UserPre.java)(不工作)

    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();
}
}
});

最佳答案

您可以添加一个 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);
}
});

关于Android (Firestore) : Unable to add or update or set (SetOptions. 合并) 已在 Firestore 中创建的文档中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com