Rodney Joll
Rodney Joll

Reputation: 55

How do I search in Firebase Database

How do I search for a user in unity using Firebase Database without using Elastic Search?

So I found this Tutorial which helped me out on Android Studio And I would like to know how can I replicate this for Unity?

(My Terrible Attempt)

public void SearchUser()
{
    FirebaseDatabase.DefaultInstance.RootReference.Child("user_id").OrderByChild("username").StartAt(searchField.text).EndAt(searchField.text + "\uf8ff")
          .GetValueAsync().ContinueWith(task => {
    if (task.IsFaulted) {
      // Handle the error...
    }
    else if (task.IsCompleted) {
      DataSnapshot snapshot = task.Result;
                  // Do something with snapshot...
                  Debug.Log("Found " +snapshot.Value.ToString());
    }
  });

}

Upvotes: 0

Views: 1503

Answers (1)

MomasVII
MomasVII

Reputation: 5061

I'm no expert but I have been following this documentation (https://firebase.google.com/docs/database/unity/retrieve-data)

FirebaseDatabase.DefaultInstance
    .GetReference("users").OrderByChild("username").EqualTo(searchusername)
    .ValueChanged += HandleValueChanged;
}

void HandleValueChanged(object sender, ValueChangedEventArgs args) {
  if (args.DatabaseError != null) {
    Debug.LogError(args.DatabaseError.Message);
    return;
  }
  // Do something with the data in args.Snapshot
}

I think you need EqualTo(searchusername) somewhere.

Upvotes: 1

Related Questions