How to check if the field exists in firestore?

前端 未结 2 1231
长发绾君心
长发绾君心 2021-01-23 01:16

I am checking whether the Boolean field called attending exists, however I am not sure how to do that.

Is there a function such as .child().exists()<

相关标签:
2条回答
  • 2021-01-23 01:32

    What you're doing now is correct - you have to read the document and examine the snapshot to see if the field exists. There is no shorter way to do this.

    0 讨论(0)
  • 2021-01-23 01:38

    You can do the following:

      if(task.isSuccessful()){
        for(QueryDocumentSnapshot document: task.getResult()){
           if (document.exists()) {
              if(document.getBoolean("attending") != null){
                 Log.d(TAG, "attending field exists");
              }
            }
          }
      }
    

    From the docs:

    public boolean exists ()

    Returns true if the document existed in this snapshot.

    0 讨论(0)
提交回复
热议问题