0

I am facing an issue in retrieving a data present in node

Dev->Regis->[userId("hjgsfgsdfghd8ydfsw3r")]->UserInfo->{userName:"", email:""}

I am trying the following query

 ref.child(kChild).child(kRegistration).queryOrdered(byChild: kUserInfo).queryEqual(toValue: searchText).observeSingleEvent(of: .value) { (snapshot) in 
       if snapshot.exists() {    
            }
        }

I also tries by making rules but it does make result

"Development": {
    "Registration": {
      "$user_id": {
            "UserInfo": {
                    ".indexOn": ["userName"],
                ".read": "auth.uid == $user_id"
                    }
            }
     }
  }

database structure

see the structure

and ref means Database.refrence()

Please help to sort out this problem.

Thanks.

1 Answers1

0

Assuming searchText is the username value that you want to grab the object for. Firebase requires an orderBy call.

let searchText = //username you wish to fetch
let ref = db.ref('Development/Registration/${userId}/userInfo')
ref.orderByChild('userName').equalTo(searchText).once("value", function(snapshot) {
    if(snapshot.exists()){
       //whatever you want to do here
    }
}
Saccarab
  • 1,399
  • 12
  • 25
  • I want to search in entire Registration node not for particular UserInfo node. – Bajrang Sinha Aug 18 '18 at 14:38
  • you cant do double nested queries as far as i know. Also check this post which explains a similar situation https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Saccarab Aug 18 '18 at 14:47
  • NO, firebase changed. this feature is added. By the way thanks for link you above sent. By that link I solved my problem. yippee. A Big Thank you – Bajrang Sinha Aug 18 '18 at 15:13