Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find a user in a ParseUser object

Hi I was wondering why this code does not work. I am trying to query a ParseUser's username field to find a certain user but it keeps saying that it cant find it.

private void findUserName(String user) {
        // query the User database to find the passed in user
        ParseQuery query = ParseUser.getQuery();
        query.whereEqualTo("username", user);
        query.findInBackground(new FindCallback() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                foundUser = (objects.size() != 0);
            }
        });
    }

Here is my method that calls it

if (!foundUser) {
    errorMessage.setText("Invalid user name");
}

foundUser is a field because I couldnt return it in the method...

like image 318
James Avatar asked Dec 05 '22 11:12

James


2 Answers

Parse treats User objects separate from Parse objects. You should use List<ParseUser> instead of List<ParseObject>. The Parse Android Guide provides an example https://parse.com/docs/android_guide#users-querying. Here is the Parse example with your where clause.

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", user);
query.findInBackground(new FindCallback<ParseUser>() {
  public void done(List<ParseUser> objects, ParseException e) {
    if (e == null) {
        // The query was successful.
    } else {
        // Something went wrong.
    }
  }
});
like image 60
Joe Avatar answered Jan 12 '23 00:01

Joe


İf you want to get current user this may be helpful;

String currentUser = ParseUser.getCurrentUser().getUsername();
like image 38
user3499689 Avatar answered Jan 11 '23 23:01

user3499689