【问题标题】:Firebase won't let me add new fieldsFirebase 不允许我添加新字段
【发布时间】:2021-03-26 12:47:46
【问题描述】:

我正在使用 java 语言开发一个 android 应用程序,并且我正在使用 firebase 进行数据存储。它以前工作正常,但最近我在文档中添加了新字段。我正在通过快照侦听器获取该文档的实时更新。但是当我尝试从DocumentSnapshot 对象中获取新字段时,它返回null。仅适用于新字段。请注意,我没有直接在数据库中添加字段。我只修改了快照侦听器中的代码以获取这些字段。此外,该文档最初不存在,但 doc.exists() 返回 true。 这是快照监听器的代码。我做错了什么?

public LiveData<List<Booking>> getBookingsDataWithIds(List<String> bookingIds){
    if(clientBookings.getValue()==null && bookingIds.size()>0){

       db.collection("Bookings").whereIn("Booking-ID",bookingIds).addSnapshotListener(new EventListener<QuerySnapshot>() {
           @Override
           public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
               if(error!=null){
                   Log.e("BOOKINGS ERROR: ",error.getMessage());
               }
               if(value!=null){
                   List<DocumentSnapshot> docs=value.getDocuments();
                   bookings=new ArrayList<>();
                   for(DocumentSnapshot doc : docs){
                       if(!doc.exists())
                           continue;
                       bookings.add(new Booking(doc.getId(),
                               doc.get("Status").toString(),
                               doc.get("Service Station").toString(),
                               doc.get("Client Name").toString(),
                               doc.get("Client Phone").toString(),
                               doc.get("Client Email").toString(),
                               (Double)doc.get("Lat"),
                               (Double)doc.get("Lng"),
                               doc.get("Vehicle Name").toString(),
                               doc.get("Vehicle Type").toString(),
                               doc.get("Date").toString(),
                               doc.get("Time").toString(),
                               doc.get("Admin Email").toString(),
                               doc.get("Payment Status").toString(),
                               doc.get("Payment Date").toString(),
                               doc.get("Payment Time").toString(),
                               (Integer) doc.get("Payment Charges"),
                               (Integer)doc.get("Payment Tip")));
                   }
                   clientBookings.postValue(bookings);
               }
               else{
                   Log.e("BOOKINGS ERROR:","value is null");
               }
           }
       });
    }
    return clientBookings;
}

【问题讨论】:

  • 如果新属性之前不存在,我的好方法是在尝试读取它们时始终检查无效。
  • @AlexMamo 是的,这实际上是更好的方法。虽然我解决了它,但我最初在参数中传递了 null 来代替新字段,并通过应用程序在“Bookings”集合中添加数据,然后将这些新字段添加回代码中:D。它有效,但您的方法更简洁。谢谢!
  • 很高兴听到这个消息。不客气;)
  • @TahaKZed 你介意用你找到的解决方案创建一个回复吗?
  • @Roger 抱歉回复晚了。我刚刚添加了解决方案作为这个问题的答案。

标签: java android firebase google-cloud-firestore


【解决方案1】:

所以,我通过删除代码来获取新添加的字段来解决它。然后我运行了一次应用程序并重新添加了代码来获取这些字段。例如,在我的代码中,假设“付款状态”字段是我添加到 firebase 的新字段。所以现在我想获取那个字段。但最初我不能这样做,因为它向我显示了错误,所以我删除/评论了代码 doc.get("Payment Status") 并运行了该应用程序。然后我重新添加了这一行并再次运行它,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2023-04-10
    • 2021-07-08
    相关资源
    最近更新 更多