当我要在用户文档中更新recipIds
数组列表时,将擦除数组列表并设置新值。
我试图获取旧的收藏,然后向其添加新的价值并进行更新,但这似乎有些过分。
public void saveRecipeIds(User user) {
String currentUserId = mAuth.getCurrentUser().getUid();
Map<String, Object> userMap = new HashMap<>();
userMap.put("recipeIds", user.getRecipesIds());
db.collection("users").document(currentUserId).update(userMap);
}
我想用新值更新我的数组列表,而又不清除旧列表数据。
这是在Firestore中的外观:
编辑:
public void saveRecipeIds(User user) {
String currentUserId = mAuth.getCurrentUser().getUid();
Map<String, Object> userMap = new HashMap<>();
userMap.put("recipeIds", user.getRecipesIds());
db.collection("users").document(currentUserId)
.update("recipeIds", FieldValue.arrayUnion(userMap));
}
已编辑
public void saveRecipeIds(User user) {
String currentUserId = mAuth.getCurrentUser().getUid();
// Map<String, Object> userMap = new HashMap<>();
// userMap.put("recipeIds", user.getRecipesIds());
db.collection("users").document(currentUserId)
.update("recipeIds", FieldValue.arrayUnion(user.getRecipesIds()));
}
错误:
java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@18.2.0:293)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@18.2.0:261)
at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:firebase-firestore@@18.2.0:202)
at com.google.firebase.firestore.UserDataConverter.parseArrayTransformElements(com.google.firebase:firebase-firestore@@18.2.0:440)
at com.google.firebase.firestore.UserDataConverter.parseSentinelFieldValue(com.google.firebase:firebase-firestore@@18.2.0:346)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@18.2.0:248)
at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:firebase-firestore@@18.2.0:202)
at com.google.firebase.firestore.UserDataConverter.parseUpdateData(com.google.firebase:firebase-firestore@@18.2.0:176)
at com.google.firebase.firestore.DocumentReference.update(com.google.firebase:firebase-firestore@@18.2.0:213)
at com.example.cookingapp.Repository.UserRepository.saveRecipeIds(UserRepository.java:143)
at com.example.cookingapp.ViewModels.UserViewModel.saveRecipeIds(UserViewModel.java:57)
at com.example.cookingapp.Views.Fragments.Home.FinishRecipeFragment$1.onClick(FinishRecipeFragment.java:111)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:2)
要更新recipeIds
数组,您应该像下面的代码行一样使用FieldValue.arrayUnion
:
db.collection("users").document(currentUserId)
.update("recipeIds", FieldValue.arrayUnion("newRecipeId"));
编辑:
为了能够更新数组(例如,通过添加新元素),您需要将字符串(id本身)传递给arrayUnion()
方法。您无法传递列表或字符串数组。您无法做到这一点。您可以一个一个地添加/删除数组中的元素。
如果要使用另一个阵列更新该阵列,则应首先从数据库中获取文档,在客户端进行必要的更改,然后将其写回到数据库中。