我正在尝试获取createdAt等于的所有条目Today,但是它什么也不返回。
createdAt
Today
我在这里做错了什么?而且,以我尝试的方式查询数据的正确方法是什么?
JSON:
{ "thoughts" : { "-KWGdcdZD8QJSLx6rSy8" : { "createdAt" : "Tomorrow", "thought" : "meno", "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2" }, "-KWGeGivZl0dH7Ca4kN3" : { "createdAt" : "Today", "thought" : "meno", "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2" }, "-KWGeIvWHBga0VQazmEH" : { "createdAt" : "Yesterday", "thought" : "meno", "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2" } } }
迅速:
let db = FIRDatabase.database().reference().child("thoughts") let ref = db.queryEqual(toValue: "Today", childKey: "createdAt") ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in for snap in snapshot.children { print((snap as! FIRDataSnapshot).key) } })
您需要使用queryOrderedByChildto,createdAt而不是使用equalToToday
queryOrderedByChild
let ref = FIRDatabase.database().reference().child("thoughts").queryOrdered(byChild: "createdAt").queryEqual(toValue : "Today") ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in for snap in snapshot.children { print((snap as! FIRDataSnapshot).key) } })