Java Firebase Fire store数据库将所有数据替换为单个数据
我有一个错误,当用户上传他的图像时,其他数据都消失了,只有配置文件URI可见 目前,我有5个字符串:Java Firebase Fire store数据库将所有数据替换为单个数据,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我有一个错误,当用户上传他的图像时,其他数据都消失了,只有配置文件URI可见 目前,我有5个字符串: 名字 电子邮件 密码 配置文件(图像URI) 参考id 硬币 但是,当用户上传他们的个人资料照片时,所有数据都被删除,只显示个人资料图像URI 我在此附上代码: 私有void updateUserProfile(){ Map profile=newhashmap(); profile.put(“profile”,imageUri.toString()); 数据库 .collection(“
- 名字
- 电子邮件
- 密码
- 配置文件(图像URI)
- 参考id
- 硬币
私有void updateUserProfile(){
Map profile=newhashmap();
profile.put(“profile”,imageUri.toString());
数据库
.collection(“Users”)//用户的路径。
.document(FirebaseAuth.getInstance().getUid())//在当前用户中更新。
//.更新(用户)
.set(配置文件)
.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公开作废(作废未使用){
Toast.makeText(getContext(),“照片更新!”,Toast.LENGTH_SHORT.show();
}
});
}
如果调用.set(profile)
,您将用profile
中的任何内容替换现有数据。如果要将profile
中的值与文档中的任何现有数据合并,请改用.update(profile)
如果您只想更新单个字段,而不需要使用任何映射值,请参阅上的Firebase文档
firestore.collection("YOUR_COLLECTION").document(user.getUid()).update("profile", imageUri.toString())
.addOnCompleteListener(task -> {
});
谢谢你的回答。我已经解决了这个问题。我已附上下面的代码 私有void updateUserProfile(){
Map profile=newhashmap();
profile.put(“profile”,imageUri.toString());
final DocumentReference sDoc=database.collection(“Users”).document(FirebaseAuth.getInstance().getUid());
database.runTransaction(new Transaction.Function(){
@凌驾
public Void apply(@NonNull Transaction Transaction)引发FirebaseFirestoreException{
DocumentSnapshot snapshot=transaction.get(sDoc);
交易记录更新(sDoc,档案);
//transaction.update(sDoc,“name”,binding.nameBox.getText());
返回null;
}
谢谢你的回答..我已经从firebase开发者文档中的firebase更新数据中得到了它。
Map<String, Object> profile = new HashMap<>();
profile.put("profile", imageUri.toString());
final DocumentReference sDoc = database.collection("Users").document(FirebaseAuth.getInstance().getUid());
database.runTransaction(new Transaction.Function<Void>() {
@Override
public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(sDoc);
transaction.update(sDoc, profile);
// transaction.update(sDoc, "name", binding.nameBox.getText() );
return null;
}