使用Swift 3.0查询Firebase

时间:2022-01-28 19:46:38

I am trying to get all the entries where createdAt is equal to Today, but it returns nothing.

我正在尝试获取createdAt与今天相等的所有条目,但它不返回任何内容。

What am I doing wrong here? And, what is the proper way to query data the way I am trying to do?

我在这里做错了什么?那么,按照我正在尝试的方式查询数据的正确方法是什么呢?

JSON:

JSON:

    {
      "thoughts" : {
        "-KWGdcdZD8QJSLx6rSy8" : {
          "createdAt" : "Tomorrow",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        },
        "-KWGeGivZl0dH7Ca4kN3" : {
          "createdAt" : "Today",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        },
        "-KWGeIvWHBga0VQazmEH" : {
          "createdAt" : "Yesterday",
          "thought" : "meno",
          "user" : "ET9tYfHqThNTsLG4bZGIbuLGauu2"
        }
      }
    }

Swift:

迅速:

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

1 个解决方案

#1


10  

You need to use queryOrderedByChild to createdAt and than use equalTo Today

您需要使用queryOrderedByChild来创建并使用equalTo

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

#1


10  

You need to use queryOrderedByChild to createdAt and than use equalTo Today

您需要使用queryOrderedByChild来创建并使用equalTo

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