0

Im trying to search for values past two ChildByAutoIDs.

See firebase structure:

enter image description here

The Value I'm using to search is "-M0p0kDIDU0v2nPXAgKN".

enter image description here

Heres my code:

func fetchBuyer(search: String, completion: @escaping (Error?) -> ()) {

    let ref = Database.database().reference().child("posts")
    let query = ref.queryOrdered(byChild: "id").queryStarting(atValue: search).queryEnding(atValue: "\(search)\\uf8ff")
            query.observeSingleEvent(of: .value) { (snapshot) in
            guard snapshot.exists() != false else {
                print("failing to search")
                return }
                print("Search value is here!", snapshot.value!)

        }

}

.

My goal is to grab the values under purchases: "-M0p0tihPq2b5-FFiPaK" and "-M0p17NP11K7MPndpin5".

But I keep getting the error "Failing to search"??

Im following an example on StackOverflow, but I still cant get it to work!

Please help!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Dan.code
  • 431
  • 2
  • 14
  • Firebase Realtime Database queries run on the direct child nodes under a location, and can filter those nodes on a property value at a fixed path under each child node. In other words: there can only be one unknown key in the path that you're looking for. To allow searching the purchases, you'll need to store the purchases in a flat list. See https://stackoverflow.com/q/27207059, https://stackoverflow.com/q/40656589, https://stackoverflow.com/a/60229824 and more from this list: https://stackoverflow.com/search?tab=newest&q=%5bfirebase-realtime-database%5d%20double%20nested – Frank van Puffelen Feb 24 '20 at 03:44
  • This question already has an answer here: https://stackoverflow.com/questions/60221546/how-does-indexon-works-with-users-and-unique-keys – Anand Prakash Feb 24 '20 at 04:52
  • The question is closed but the issue is the structure is too deep for the query. Your query is querying one level, */posts/some_id/id_your_querying_for*, but your structure is */posts/some_id/another_id/id_your_querying_for*. It's not clear why your data is deeply nested but it could be corrected by denormalizing your data (flattening). Also, please include structures as *text*, not links and images. That way, if they are needed in an answer, they can be copied and pasted. To get your Firebase structure, use the Firebase console->Export JSON – Jay Feb 24 '20 at 21:07

0 Answers0