how to fetch ParseUser object by objectId? - android

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.

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

How to query a pointer in a ParseObject class?

Hi i made ParseObject which contains a field called Usuario which is a pointer to _User(ParseUser class)
ive been trying to query for this value with no success...
private void queryConversaciones(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
mensaje=obj.getString("Mensaje");
enviaMensaje= (ParseUser) obj.get("Usuario");
// mMessageList.add(obj);
}
}
});
}
Just add query.include("key"); to retrieve the pointer object.
So your complete query should look like this:
private void queryConversaciones(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
// Include data at pointer
query.include("Usuario");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
mensaje=obj.getString("Mensaje");
enviaMensaje= (ParseUser) obj.get("Usuario");
// mMessageList.add(obj);
}
}
});
}
Now you should be able to access all the data stored in the _User object.
I got all this information from the relational queries section in the Parse Android guide.

get objectId parse android

I have done storing into database and but when I wish to retrieve the database elements, I always need to specify the objectID which needs to be checked in database. I want a way by which I can get objectID from the element or better get the object id while storing.
"o1l5gCCPB4" is the objectID
Here is code I used for retrieval:
query.getInBackground("o1l5gCCPB4", new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
String playerName = object.getString("foo");
tv.setText(playerName);
} else {
// something went wrong
tv.setText("Something went wrong!!");
}
}
});
You can get an element, this way:
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(PARSE_CLASS_DRAGS);
query.whereEqualTo("objectId", objectId);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
//convert the parseObjects to your objects
}
});
If you want to get all of the elements, you only have to ignore the query.whereEqualTo() row.

Add two users to parse object and Query

I want to create a parse object with two users and query for the object using both users. My get other user method returns the other user i want to add to the group.
String id;
final ParseUser[] user = new ParseUser[1];
public void getOtherUser()
{
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "amanda");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
user[0] = objects.get(0);
addUserGroup();
} else {
// Something went wrong.
}
}
});
}
my add userGroup created a parse object with both users
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
After ive done this i want to be able to be able to update the parse object with new content and query for the object based on both users. The following mehtod is breaking on me though and im not sure why.
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
Its returning the following error
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Lcom.parse.ParseUser;
anybody know why this might be happening?
When you call ParseUser.getCurrentUser() parse return a user object. Now assuming you want to query usernames you need to change the ParseUser.getCurrentUser() to ParseUser.getCurrentUser().getUsername()
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser().getUsername());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser().getUsername() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
}

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

Categories

Resources