首页 > 解决方案 > 数据存在时,Swift firebase 快照返回 null

问题描述

我试图简单地从我的firebase数据库中获取所有通知,这些通知的子值等于我的postId。我知道它存在的事实,如果我尝试几次,它最终会返回快照。这里可能是什么问题?

let ref = Database.database().reference().child("notification").child(postUserId).queryOrdered(byChild: postId).queryEqual(toValue: postId)
        ref.observeSingleEvent(of: .value) { (snapshot) in
            print(snapshot.value)
        }

数据库

标签: iosswiftfirebasefirebase-realtime-database

解决方案


改变这个:

let ref = Database.database().reference().child("notification").child(postUserId).queryOrdered(byChild: postId).queryEqual(toValue: postId)

进入这个:

let ref = Database.database().reference().child("notification").child(postUserId).queryOrdered(byChild: "postId").queryEqual(toValue: postId)

postId在查询中添加引号


推荐阅读