一尘不染

使用Swift 3.0查询Firebase

swift

我正在尝试获取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)
        }
    })

阅读 211

收藏
2020-07-07

共1个答案

一尘不染

您需要使用queryOrderedByChildto,createdAt而不是使用equalToToday

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)
    }
})
2020-07-07