How to get user_id from nested parent base

I want to get information about sites based on user_id, I use the following query to get site information.

ref = Database.database().reference()

        self.ref.child("sites").queryOrdered(byChild: "user_id").queryEqual(toValue:"DxMYXkVqnZMC6NEZxdSQF3G2ZJ53")
            .observe(.childAdded, with: { snapshot in

                if ( snapshot.value is NSNull ) {
                    print("Not found")


                } else {
                    print("found")


                }
            })

      

I can trigger a request if its store_name, idToken, etc. But failed to call user_id because its inside childbuAutoid

enter image description here

+3


source to share


3 answers


You can create a new value listener for "users" and then process each child accordingly, or you can use a for loop to loop over the children, for example:



for child in snapshot.childSnapshot(forPath: "users").children

      

0


source


Since your user id is nested, you need to keep looping through your data, so it looks ugly. Take a look at the following code, it should work. It just iterates over the values ​​until it finds the user_id. For more information on images, see my other answer: fooobar.com/questions/2421663 / ... This makes it clearer that it is doing something.



ref = Database.database().reference()

        self.ref.child("sites").observe(.childAdded, with: { snapshot in

                if ( snapshot.value is NSNull ) {
                    print("Not found")


                } else {
                 print("found")
                 let enumerator = snapshot.children
                 while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                   if (rest as? DataSnapshot).key == "users"{
                   let enumerator2 = (rest as? DataSnapshot).children
                     while let rest2 = enumerator2.nextObject() as? FIRDataSnapshot {
                   for userID in (rest2 as? DataSnapshot).children{
                     let values = (userID as? DataSnapshot).value as? NSDictionary
                     let userId = values?["user_id"] as? String ?? ""
                      print(userId)
            }
         }
     }
   }
 }
})

      

0


source


If you can change your database structure a little than you don't need to use all loops. You can change your database as shown below. rather, using an autogenerated key, you can use the user id as the key and supply the value whatever you want.

enter image description here

Now you write your request like this. (Sorry! This piece of code is in java, Do not know quickly, but it will help you).

dbr.child("sites").orderByChild("users/"+/*put user_id you want to search*/).equalTo("user_id").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

           /*here you get the datasnapshot according to search*/

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

      

try updating your code.

self.ref.child("sites").queryOrdered(byChild: "users/"+"DxMYXkVqnZMC6NEZxdSQF3G2ZJ53").queryEqual(toValue:"user_id")
        .observe(.childAdded, with: { snapshot in

            if ( snapshot.value is NSNull ) {
                print("Not found")


            } else {
                print("found")


            }
        })

      

Hope this helps you.

0


source







All Articles