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.
}
}
});
Related
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.
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.
I want to sort my query first if a field is true or false, and then by updatedAt. How do I achieve this on Android with Parse SDK?
My current code is:
mQuery.orderByDescending("draft");
mQuery.addAscendingOrder("updatedAt");
But I get java.lang.IllegalArgumentException: Unable to sort by key draft.
First parse the query with draft values true with ascending order of updateTime and store the values to an array. After fetching this parse the query with draft values false.
ArrayList<ParseObject> result=new ArrayList();
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("draft", true);
query.orderByAscending("updatedAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
ParseQuery<ParseObject> query1 = ParseQuery.getQuery("_User");
query1.whereEqualTo("draft", false);
query1.orderByAscending("updatedAt");
query1.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
for(int i=0;i<object.size();i++)
{
result.add(object.get(i));
}
}
}
});
}
}
});
Try using
mquery.whereEqualTo("True","draft");
instead of orderByDescending.
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");
}
});
}
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();