简体   繁体   English

Firebase 检查项目是否存在

[英]Firebase check item if exist

I'm saving an id to Firebase Realtime Database like this:我将 id 保存到 Firebase 实时数据库,如下所示:

binding.ivStar.setOnClickListener {
        firebaseDb.getReference("Users").child(uid).child("movie").push()
            .setValue(args.movie?.id)}

And I need to check if this id exists in Firebase.我需要检查这个id是否存在于Firebase中。 And I did like this:我确实喜欢这样:

private fun checkData() {
    val postRef = firebaseDb.getReference("Users").child(uid).child("movie")

    postRef.addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            if (snapshot.exists()) {
                Log.e("Fragment", "${args.movie?.title} exists")

            } else {
                Log.e("Fragment", "${args.movie?.title} not exists")

            }
        }

        override fun onCancelled(error: DatabaseError) {
            Log.e("Fragment", error.toString())
        }
    })
}

Every time I check, it returns movie exists even if I don't save it.每次我检查时,即使我不保存它,它也会返回电影存在。 Where am I doing wrong?我在哪里做错了?

try snapshot.getValue<>() and then check if this value is null or not maybe that help尝试snapshot.getValue<>()然后检查这个值是否是 null 或者不是可能有帮助

every time I check, it returns a movie exists even if I don't save it.每次我检查时,即使我不保存它,它也会返回存在的电影。

This is happening because when you attach a listener to the following reference:发生这种情况是因为当您将侦听器附加到以下引用时:

val postRef = firebaseDb.getReference("Users").child(uid).child("movie")

It checks if the movie node exists, and not a particular movie.它检查movie节点是否存在,而不是特定电影。 Since under that node, most likely there are multiple movies, snapshot.exists() will always return true.由于在该节点下,很可能有多个电影, snapshot.exists()将始终返回 true。 If you want to check the existence of a particular movie, you need to add the corresponding ID to the reference:如果要检查特定电影是否存在,则需要在引用中添加相应的 ID:

val postRef = firebaseDb.getReference("Users")                   
                        .child(uid)
                        .child("movie")
                        .child("movieIdFromDatabase") //👈

Where movieIdFromDatabase should be the ID from the database, the one that starts with - .其中movieIdFromDatabase应该是数据库中的 ID,即以-开头的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 在数据库中发布之前检查是否存在值数组 - Firebase check if an array of values exist before posting in DB Flutter firebase 检查 email 存在于 firestore 集合中 - Flutter firebase check email exist inside firestore collection 我如何检查 email 是否已存在于身份验证 firebase 中 - how I can check if the email is already exist in authentication firebase 如何检查该字段在Firebase中是否存在,如果存在则打印其值,如果不存在则在Flutter中添加一个空值 - How to check whether the field exists in Firebase, if it exists then print its value, if it does not exist, then add it with an empty value in Flutter 如何检查 firebase 中的嵌套文档和集合是否存在,如果不存在,如何创建,反应本机 - how to check if nested docs and collection in firebase exist and if don't how to create, in react native 如何使用 flutter 检查用户是否已存在于 firebase 身份验证中? - How do I check if a user is already exist in firebase Authentication using flutter? React 和 Firebase 项目计数器 - React and Firebase item counter 如果用户不存在,React + Firebase 表单验证 - React + Firebase Form Validation if user does not exist “用户”类型不存在 Firebase updateProfile - Firebase updateProfile does not exist on type 'User' “Firebase”类型不存在属性“firestore” - Property 'firestore' does not exist on type 'Firebase'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM