数据存在时,Swift Firebase快照返回null

时间:2019-12-11 10:45:13

标签: ios swift firebase firebase-realtime-database

我正试图从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)
        }

Database

1 个答案:

答案 0 :(得分:2)

更改此:

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中添加引号