【问题标题】:Firebase Fire store database replacing all data to single oneFirebase Firestore 数据库将所有数据替换为单个数据
【发布时间】:2021-08-23 19:20:35
【问题描述】:

我有一个错误。当用户上传他的图片时。其他数据都消失了,只有配置文件 URI 可见。

目前,我有 5 个字符串

  • 姓名
  • 电子邮件
  • 密码
  • 个人资料(图片 URI)
  • 参考编号
  • 硬币

但是,当用户上传他们的个人资料照片时,所有数据都会被删除。

仅显示个人资料图片 URI

我已在此处附上代码

private void updateUserProfile() {
    Map<String,String > profile = new HashMap<>();
    profile.put("profile",imageUri.toString());

    database
            .collection("Users") // the path of users.
            .document(FirebaseAuth.getInstance().getUid()) // to update in the current users.
        //    .update(user)
            .set(profile)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {

                 Toast.makeText(getContext(), "Photo Updated!", Toast.LENGTH_SHORT).show();

                }
            });
}

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    如果您只想更新单个字段,何时可以在不使用任何 Map 值的情况下更新。

    firestore.collection("YOUR_COLLECTION").document(user.getUid()).update("profile", imageUri.toString())
                    .addOnCompleteListener(task -> {
                        
                    });
    

    【讨论】:

      【解决方案2】:

      如果您调用.set(profile),您将用profile 中的任何内容替换现有数据。如果要将profile 中的值与文档中的任何现有数据合并,请改用.update(profile)

      另请参阅 updating data in a document 上的 Firebase 文档。

      【讨论】:

      • 感谢您的回答。我已经从 firebase 开发人员文档的 firebase 更新数据中获得了它。
      【解决方案3】:

      感谢您的回答。我已经解决了这个问题。我附上了下面的代码。

      private void updateUserProfile() {

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

      【讨论】:

        猜你喜欢
        • 2018-03-20
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 2020-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多