【问题标题】:FireStore Keeps Replacing Field - AndroidFireStore 不断替换字段 - Android
【发布时间】:2018-02-22 15:44:46
【问题描述】:

我有一个活动,在用户创建帐户(身份验证)后,它会自动在 FireStore 中的用户文档中放置 complete_profile=true。为了给你更多的视角,这是我的 Firestore 的样子:

users <-- Collection
  |
  A8as7f9aKsrYas12l7 <--- User's ID/Document
         |
         complete_profile: "true" <-- Field

现在,我有另一个页面,名为 completeprofileactivity。它询问用户的姓名。之后,我想在我的用户数据name: "sample name" 中添加另一个字段。问题是添加名称后,complete_profile 消失了:

users 
  |
  A8as7f9aKsrYas12l7 
         |
         name: "sample name" <-- replaced?

当我再次使用同一个用户重新创建一个新帐户时,它应该添加complete_profile 对吗?所以我将同时拥有namecomplete_profile。但相反,我又得到了这个:

users 
  |
  A8as7f9aKsrYas12l7 
         |
         complete_profile: "true" <-- it replaced the "name" again

我的完整个人资料有此代码:

Boolean userdoComplete = true;
String str_userid = mAuth.getCurrentUser().getUid();

Map<String, String> user_map = new HashMap<>();
    user_map.put("complete_profile",userdoComplete.toString());

    mFirestore.collection("users").document(str_userid).set(user_map).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if(task.isSuccessful()){
                                    //Registered Successfully
                                    //Intent to Main Activity
                                    Intent goMain = new Intent(SignUpActivity.this, MainActivity.class);
                                    startActivity(goMain);

                                } else {

                                }
                            }
                        });

以及添加名称:

String str_userid = mAuth.getCurrentUser().getUid();
Map<String, String> specialMap = new HashMap<>();
        specialMap.put("name", name);
        mFireStore.collection("users").document(str_userid).set(specialMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    //All is Successful - Proceed
                    Toast.makeText(AddSpecialActivity.this, "Successful!", Toast.LENGTH_SHORT).show();

                } else {
                    //FireStore Error
                    Toast.makeText(AddSpecialActivity.this, "There was an Error. Please Try Again.", Toast.LENGTH_LONG).show();
                }
            }
        });

我不明白为什么一个人总是替换另一个人。它应该同时添加complete_profilename

【问题讨论】:

    标签: android firebase firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    如您所见,这段代码:

    mFireStore.collection("users").document(str_userid).set(specialMap)
    

    sn-p 替换了str_userid 引用的文档中的所有内容。这当然是预期的行为,因为您将文档设置为您的specialMap。他们很容易解决这个问题,即使用merge() SetOption

    mFireStore.collection("users").document(str_userid).set(specialMap, SetOptions.merge())
    

    有关更多信息,您可以查看Firestore documentation here

    【讨论】:

      【解决方案2】:

      set 替换整个文档。您应该考虑改用 update 调用来更新或向现有文档添加字段。

      https://firebase.google.com/docs/firestore/manage-data/add-data#update-data

      【讨论】:

      • 我试过.update(specialMap),它给了我一个错误,说“它不能应用于”
      猜你喜欢
      • 2020-12-28
      • 1970-01-01
      • 2022-01-02
      • 2011-09-21
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 2012-04-21
      • 2017-09-13
      相关资源
      最近更新 更多