2
認證期間,用戶使用電子郵件和用戶名創建。現在我試圖用註冊時間上的名字,姓氏,地址等新字段來更新該用戶。但是,當我嘗試插入新字段時,它會使用新字段進行更新,並刪除舊字段。如何使用Firebase實時數據庫中的新字段更新子項?
public class User {
String uid,userName,firstName,lastName,email;
public User() {
}
//called on the time of auth
public User(String email, String userName) {
this.email = email;
this.userName = userName;
}
//called on registration process
public User(String firstName, String lastName,String mobileNo) {
this.firstName = firstName;
this.lastName = lastName;
this.mobileNo = mobileNo;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("email", email);
result.put("userName", userName);
result.put("firstName", firstName);
result.put("lastName", lastName);
return result;
}
以下方法用於添加和更新Firebase數據庫。 addUser方法功能正常,但在更新方法期間,它會替換舊數據。
String userId = getUid(); // its retrun firebase current user id as I use
// auth authentication
//first time entry in database
private void writeNewUser(String name, String email) {
User user = new User(name, email);
Map<String, Object> postValues = user.toMap();
mDatabase.child("users").child(userId).setValue(postValues);
}
//Its called during the registration porecess
private void updateUser() {
User user = new User(firstName, lastName, email);
Map<String, Object> postValues = user.toMap();
mDatabase.child("users").child(userId).updateChildren(postValues);
}
可以顯示更新數據庫的代碼嗎? – faruk
@faruk請檢查更新 –