gpt4 book ai didi

java - 无法从 firestore 中删除字段

转载 作者:行者123 更新时间:2023-12-01 16:13:10 27 4
gpt4 key购买 nike

我无法从 firestore 中删除字段

else if(starPosition == 0){
Map<String, Object> deleteRating = new HashMap<>();
deleteRating.put(inititalRating + "_star", documentSnapshot.getLong(inititalRating + "_star") - 1);
deleteRating.put("average_rating", String.valueOf(calculateAverageRating(starPosition-inititalRating,true)));
deleteRating.put("total_ratings", documentSnapshot.getLong("total_ratings") - 1);

firebaseFirestore.collection("PRODUCTS").document(productID)
.update(deleteRating).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Map<String, Object> ratingUpdate = new HashMap<>();
ratingUpdate.put("list_size", DBqueries.myRatedIds.size() - 1);
ratingUpdate.put("product_ID_" + DBqueries.myRatedIds.size(), FieldValue.delete());
ratingUpdate.put("rating_" + DBqueries.myRatedIds.size(), FieldValue.delete());

firebaseFirestore.collection("USERS").document(currentUser.getUid()).collection("USER_DATA").document("MY_RATINGS")
.update(ratingUpdate).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {

}
});


}
}
});

}

该字段未删除,我在这里使用 FieldValue.delete()

Screenshot firestore fielsd

最佳答案

你的柜台少了一位。你运行:

   Map<String, Object> ratingUpdate = new HashMap<>();
ratingUpdate.put("list_size", DBqueries.myRatedIds.size() - 1);
ratingUpdate.put("product_ID_" + DBqueries.myRatedIds.size(), FieldValue.delete());
ratingUpdate.put("rating_" + DBqueries.myRatedIds.size(), FieldValue.delete());

firebaseFirestore.collection("USERS").document(currentUser.getUid()).collection("USER_DATA").document("MY_RATINGS")

由于屏幕截图显示 list_size 为 0,这意味着 DBqueries.myRatedIds.size() 为 1。因此您要删除 product_ID_1 rating_1 字段,这些字段确实不存在/不再存在于文档中。

如果您想删除 0 索引字段,则还需要使用 DBqueries.myRatedIds.size() - 1

关于java - 无法从 firestore 中删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62481555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com