【问题标题】:Create collection if it does not exists如果集合不存在则创建集合
【发布时间】:2021-03-29 21:11:54
【问题描述】:

我正在尝试创建一个集合,如果它在 firestore 中不存在。

我的代码如下

 val docRef = fbase.collection("cities").document("LA")
    docRef.get().addOnSuccessListener {
            document ->
        if(document!= null){
            Toast.makeText(this, "Already exists", Toast.LENGTH_LONG).show()
        }
        else{
            FireBaseRepository 
            Toast.makeText(this, "Does not exists", Toast.LENGTH_LONG).show()
        }
    }

“FireBaseRepository”只是一个带有初始化集合的类。我的代码总是通过 if 条件,因为 FirebaseFirestore.java 类确保集合不能为空。我已经看过推荐的链接https://firebase.google.com/docs/firestore/query-data/get-data?hl=en

 @NonNull
  public CollectionReference collection(@NonNull String collectionPath) {
    checkNotNull(collectionPath, "Provided collection path must not be null.");
    ensureClientConfigured();
    return new CollectionReference(ResourcePath.fromString(collectionPath), this);
  }

【问题讨论】:

    标签: android kotlin google-cloud-firestore


    【解决方案1】:

    您必须检查以下代码

            if(!document.exists()){
                Toast.makeText(this, "Does not exists", Toast.LENGTH_LONG).show()
                FireBaseRepository
            }
            else{
                Toast.makeText(this, "Already exists", Toast.LENGTH_LONG).show()
            }
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2017-04-29
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2020-07-03
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多