Firestoreでdocumentが存在するか確認する方法を教えてください。
言語はKotolinです。
スタック・オーバーフローはプログラマーとプログラミングに熱心な人のためのQ&Aサイトです。すぐ登録できます。
登録してこのコミュニティに参加するDocumentSnapshotの exists()
から確認できます。Documentが存在しない場合には getData()
などを用いてもnullが返されるので、それから判断することも可能です。
If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists().
val docRef = db.collection("cities").document("SF")
docRef.get()
.addOnSuccessListener { document ->
if (!document.exists()) {
Log.d(TAG, "DocumentSnapshot data: ${document.data}")
}
...
}
.addOnFailureListener { exception ->
...
}
}