无法使用userId更新Firebase用户表 [英] Unable to Update Firebase user table with userId
问题描述
在这里,我在注册过程中使用"firstName","lastName","email","password"创建了一个"Firebase"用户.成功登录后,我将所有注册值加载到带有新的两个"Edittext"的另一个屏幕上.现在,我需要使用新的两个"country"和"occupation"值在"Firebase""userId"中更新"Firebase""userId"为此,我使用了"UserProfileBean"类.
In here I create a 'Firebase' user in the signup process with 'firstName', 'lastName', 'email', 'password'. After successful login I load all signup values to another screen with new two "Edittext'. Now I need to update that signup table by 'Firebase' 'userId' with new two values 'country' and 'occupation' that is entered in new 'EditText'. For do that signup process and add new values I used "UserProfileBean' class.
下面是代码.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("new_user");
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
final UserProfileBean userProfileBean = new UserProfileBean();
userProfileBean.setCountry(country.getText().toString());
userProfileBean.setOccupation(occupation.getText().toString());
Log.d("UPDATE_PROFILE", "UPDATE_PROFILE_DATA" + userid + "," +
userProfileBean.getOccupation() + ", " + userProfileBean.getCountry());
//myRef.child(userid).child("country").setValue(userProfileBean.getCountry());
Query pendingTasks = myRef.child(userid).orderByChild("country").equalTo("");
pendingTasks.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot tasksSnapshot) {
for (DataSnapshot snapshot: tasksSnapshot.getChildren()) {
snapshot.getRef().child("country").setValue(userProfileBean.getCountry());
snapshot.getRef().child("occupation").setValue(userProfileBean.getOccupation());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
以上代码是通过"onclickListener"按钮实现的.
above code is implemented by button 'onclickListener'.
推荐答案
要更新用户节点,请尝试以下操作:
To update the user node, try the following:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("User").child(useruid);
Map<String,Object> update=new HashMap<>();
update.put("Country",userProfileBean.getCountry());
update.put("Occupation",userProfileBean.getOccupation());
ref.updateChildren(update);
更多信息在这里:
https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields
这篇关于无法使用userId更新Firebase用户表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!