【问题标题】:Instead of updating data of user the data gets deleted from firebase不是更新用户的数据,而是从 firebase 中删除数据
【发布时间】:2019-05-14 09:41:58
【问题描述】:

我使用 Firebase 创建了一个登录注册系统,在个人资料页面中,我从 Firebase 检索了用户的数据接下来,我为我的用户创建了一个编辑个人资料页面,用户可以在其中编辑信息和个人资料图片,但这是我的问题

当用户点击保存按钮时,该特定用户的整个节点将从 firebase 中删除。这是我的代码

编辑 profile.java

 public class ProfileEditInfo extends AppCompatActivity {
private EditText newUserName, newUserEmail, newUserPhone;
private Button save;
private DatabaseReference databaseReference;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private ImageView updateProfilePic;
private static int PICK_IMAGE = 123;
Uri imagePath;
private StorageReference storageReference;
private FirebaseStorage firebaseStorage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_edit_info);
    updateProfilePic = findViewById(R.id.ivProfileUpdate);
    newUserName = findViewById(R.id.etNameUpdate);
    newUserEmail = findViewById(R.id.etEmailUpdate);
    newUserPhone = findViewById(R.id.etPhoneUpdate);
    save = findViewById(R.id.btnSave);
    databaseReference= FirebaseDatabase.getInstance().getReference("users");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    String userID = firebaseAuth.getUid();
    firebaseStorage = FirebaseStorage.getInstance();

    storageReference = FirebaseStorage.getInstance().getReference();

    final StorageReference storageReference = firebaseStorage.getReference();
    storageReference.child("images").child(firebaseAuth.getUid() + "." + "jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            Picasso.get().load(uri).fit().centerCrop().into(updateProfilePic);
        }
    });

    // final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userID);
    databaseReference.child(userID).addListenerForSingleValueEvent(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Users userProfile = dataSnapshot.getValue(Users.class);

            newUserName.setText(userProfile.getName());
            newUserPhone.setText(userProfile.getPhone());
            newUserEmail.setText(userProfile.getEmail());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(ProfileEditInfo.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
        }

    });



    save.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            String name = newUserName.getText().toString();
            String phone = newUserPhone.getText().toString();
            String email = newUserEmail.getText().toString();
            //     Users userProfile = new Users(name, phone, email);
            //     databaseReference.setValue(userProfile);




            String id = firebaseAuth.getCurrentUser().getUid();
            Users user = new Users(id,name, email, phone);
            databaseReference.child(id).setValue(user);

            StorageReference imageReference = storageReference.child("images").child(firebaseAuth.getUid() + "." + "jpg");  //User id/Images/Profile Pic.jpg
            UploadTask uploadTask = imageReference.putFile(imagePath);

            uploadTask.addOnFailureListener(new OnFailureListener() {
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(ProfileEditInfo.this, "Upload failed!", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(ProfileEditInfo.this, DisplayProfile.class));
                    finish();
                }

            }).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    Toast.makeText(ProfileEditInfo.this, "Upload successful!", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(ProfileEditInfo.this, DisplayProfile.class));
                    finish();

                }

            });

            finish();
        }
    });

    updateProfilePic.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE);

        }

    });


}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getData() != null) {
        imagePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
            updateProfilePic.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    super.onActivityResult(requestCode, resultCode, data);
} 
}

users.java 的代码在这里

Users.java

   package com.example.deepak.myapplication;

public class Users {  
public String imageUrl;
public String imageName;
public String uid;
public String name;
public String email;
public String password;
public String phone;

public Users(){}

public Users(String id, String name, String phone, String email){}
public Users(String uid, String name, String email, String password, String phone) {
    this.uid = uid;
    this.name = name;
    this.email = email;
    this.password = password;
    this.phone = phone;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
}

数据库结构在这里

数据库结构

当我单击编辑信息时,我能够检索编辑文本框中的所有数据,但是一旦我编辑数据并单击保存按钮,我创建用户的用户 ID 的整个节点将被删除,然后我得到空指针异常

所以请帮助 e,让我知道我哪里出错了 谢谢。

【问题讨论】:

  • 您确定您在 Firebase 数据库中的数据已被删除,因为从您的代码中我看到,当您点击 imageView 时,您只需打开新活动并设置新图片。可能是我没有正确理解您的问题
  • 是的,我在笔记本电脑的屏幕上打开了 firebase 实时数据库,然后我从手机上点击了保存按钮,它会在下一秒被删除
  • 是刚刚被删除还是发生了其他事情?什么时候应该更新您的数据?
  • 我不知道发生了什么我所看到的只是当我点击保存按钮时我的节点正在从实时数据酶中删除,而当我点击保存按钮时它应该得到更新
  • 所有节点都被删除了还是只有下面的几个属性?请回复@AlexMamo

标签: android firebase firebase-realtime-database


【解决方案1】:

Firebase 要求每个节点至少有一个具有键和值的子节点。 两者。只有一个没有值的键作为子节点或根本没有子节点将导致整个节点被删除。我建议您确保在将用户对象保存到 Firebase 之前设置了值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    相关资源
    最近更新 更多