how to find a user in a ParseUser object - android

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...

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.
}
}
});

İf you want to get current user this may be helpful;
String currentUser = ParseUser.getCurrentUser().getUsername();

Related

Query a ParseObject(Pointer) in User Class

Hello i am having some trouble in a Query. I made a pointer called "Empresa" in the _User class. i understand that this pointer is a ParseObject.So i did this i tried to do it in two ways...
private void queryEmpresa(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId);
query.include("Empresa");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
and also...
private void queryEmpresa(){
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
query.include("Empresa");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
for (ParseUser obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
tell me which is the correct code and what do i need to modify in order to work. Can you please explain to me why is the reason this isnt working so that i dont incur into this problem in the near future?.
Try this and check if you get some error from the server:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.include("Empresa");
query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your user and you should be able to retrieve Empresa like this
empresa = object.getParseObject("Empresa");
} else {
// something went wrong. It would be good to log.
}
}
});

Using Parse get All User data

I use parse AnyPhone for Login using mobile number and after successfully login (OTP verification), i want to get all parse user from User table for that i used bellow query but i only get Login user Data not all. Thanks in Advance.
ArrayList<String> Email = new ArrayList<String>();
Email.add("test2#gmail.com");
Email.add("test#g.com");
Email.add("test#gmail.com");
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereContainedIn("email", Email);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if(e==null){
Log.i("Size",""+objects.size());
}
}
});
void getAllParseUser() {
ParseUser user = ParseUser.getCurrentUser();
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo(ParseTableConstants.OBJECT_ID, user.getObjectId());
progressBar.showProgress();
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
insertData(objects);
} else {
// Something went wrong.
progressBar.hideProgress();
}
}
});
}
This code will give you all parse user
I solved My Problem by setting ACL in javascript File.Now i am getting all the Users.
var acl = new Parse.ACL();
// public can read
acl.setPublicReadAccess( true );
// public cannot write
acl.setPublicWriteAccess( false );
// user can read data
acl.setReadAccess( user.id, true );
// user can write data
acl.setWriteAccess( user.id, false );
user.setACL(acl);
user.save();

how to fetch ParseUser object by objectId?

I have objectId and I want to fetch ParseUser object which has same objectId.
I have read that we can use get to get ParseUser by id at Parse documentation but syntax is not given there. If anyone know please help me.
I have tried this but it is not working.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", userId);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> object, ParseException e) {
// TODO Auto-generated method stub
if(e==null)
for(ParseUser obj: object)
{
adress.setText(obj.getString("address"));
contact.setText(obj.getString("contact"));
}
else
{
adress.setText("Not found");
}
}
});
You can use the following code to get the user;
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId",userId);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
//List contain object with specific user id.
} else {
// error
}
}
});
Hope this helps
Regards.

Android: Updating fields for ParseUsers

I am using ParseUser for user management. Having the username (unique), I planned to query the User class to identify the user and then put a new score to the "score" field, just like a Leaderboard. Coded as follows:
Code:
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseObject object, ParseException e)
{
if (e == null)
{
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
object.put("score", new_score);
object.saveInBackground();
onBackPressed();
}
});
}
}
});
}
Situation:
It states that The type new FindCallback(){} must implement the inherited abstract method FindCallback.done(List, ParseException) . I have tried that FindCallback.done and found it unsucessful.
Question:
I would like to ask how the other fields of the user be updated with the username given? Thanks!
As per the parse documentation said,
The ParseUser class is secured by default. Data stored in a ParseUser can only be modified by that user. By default, the data can still be read by any client. Thus, some ParseUser objects are authenticated and can be modified, whereas others are read-only.
Specifically, you are not able to invoke any of the save or delete type methods unless the ParseUser was obtained using an authenticated method, like logIn or signUp. This ensures that only the user can alter their own data.
The following illustrates this security policy:
ParseUser user = ParseUser.logIn("my_username", "my_password");
user.setUsername("my_new_username"); // attempt to change username
user.saveInBackground(); // This succeeds, since the user was authenticated on the device
// Get the user from a non-authenticated manner
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(user.getObjectId(), new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
object.setUsername("another_username");
// This will throw an exception, since the ParseUser is not authenticated
object.saveInBackground();
}
});
please go through this link Reference
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseUser object, ParseException e)
{
if (e == null)
{
object.put("score", new_score);
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
if(e==null)
onBackPressed();
}
});
}
}
});
}
As far as I understand from your question is; you want to find the user entry in user table via using the specified user id and update the score field. First suggestion, instead of findInBackground use getFirstInBackground. Following that check the returned user object if not null perfrom the user update operation. Then call saveInBackground. saveInBackground is used for perfroming actions when the save is succesful. In your code, I see that you try to save the object in done method again. Also, one suggestion is that control the ACL.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// No error e is null
// check user object
if (user == null) {
// no user , error
} else {
// perfrom save operation
// put score field
// call save in background
// in done method update UI for example.
}
} else {
// Something went wrong.
}
}
});

Query particular Parse user's information?

I have the objectID of a particular user (who is not the current user) and would like to find his Display Name, which is a custom field that I created.
As per Parse's guide, this code will create a list of users that match some particular attributes.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("gender", "female");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
} else {
// Something went wrong.
}
}
});
There's no reason for me to make a list, however, if only one user has that objectID. Any way for me to just get the display name of a particular user?
What you want instead is getFirstInBackground(), e.g.:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// The query was successful.
// check if we got a match
if (user == null) {
// no matching user!
} else {
// great, get the name etc
}
} else {
// Something went wrong.
}
}
});

Categories

Resources