I'm trying to modify a field for the the currentUser in Parse. I run the following code and my log keeps returning "no results found". I've logged the ObjectId for reference and verified that it's correct in my database.
ParseQuery<ParseUser> query = ParseQuery.getQuery("User");
query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
if (e == null) {
Log.i("objectId", userObjectId);
object.put("instructorId", instructorId.getText().toString());
object.saveEventually();
} else {
Log.e("objectId", "Error: " + e.getMessage());
Log.i("objectId", userObjectId);
}
}
});
}
});
Your assumption that the User class has the classname User is probably the issue. Try _User.
BUT, you should really be creating your query as
ParseQuery<ParseUser> query = ParseUser.getQuery();
That way you don't even need to know the classname for the user object.
Related
Im currently trying to populate my ListView with ParseUsers from my Parse.com database. I have tried the below code with another class and that worked fine. I dont understand why the ParseUser is returning 0 objects. Is it not allowed to query for ParseUsers because they have passwords?
void getUsers(final AdminSearchActivity adminSearchActivity){
ParseQuery<User> query = ParseQuery.getQuery(User.class);
query.findInBackground(new FindCallback<User>() {
public void done(List<User> objects, ParseException e) {
if (e == null) {
System.out.println(objects.size());
for(User u: objects){
System.out.println(u.getName());
}
adminSearchActivity.populateListView(objects);
} else {
Log.d("ParseError", e.toString());
doToastMessageInView(adminSearchActivity, "ERROR: Failed to retrieve users.");
}
}
});
}
Outputs "I/System.out: 0"
void getUsers(final AdminSearchActivity adminSearchActivity){
ParseQuery<Branch> query = ParseQuery.getQuery(Branch.class);
query.findInBackground(new FindCallback<Branch>() {
public void done(List<Branch> objects, ParseException e) {
if (e == null) {
System.out.println(objects.size());
for(Branch u: objects){
System.out.println(u.getName());
}
adminSearchActivity.populateListView(objects);
} else {
Log.d("ParseError", e.toString());
doToastMessageInView(adminSearchActivity, "ERROR: Failed to retrieve users.");
}
}
});
}
Outputs "I/System.out: 2"
Here is an image of my table overview
Found it. We used #ParseClassName("user") in our "User" class. Should've user #ParseClassName("_User"). That is why the query wasn't looking in the right table.
EDIT: kishore jethava provided a simpler solution in a reply. Havent tested that but should be easier
ParseQuery<ParseUser> queryParseUser = ParseUser.getQuery(); // kishore jethava
I've question about way in which I should create query to get expected data.
I've MyObject (ParseObject) which has pointer to ParseUser (column named createdByPtr). In ParseUser I have custom column userGroupPtr.
Now I want to get all MyObject rows which are created by all users (createdByPtr) belongs to user group (userGroupPtr) to which belongs current logged user (ParseUser.CurrentUser()).
At this moment I have some kind of draft like this:
ParseQuery<MyObject> query = ParseQuery.getQuery(MyObject.class);
query.include("createdByPtr");
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
query.orderByDescending("updatedAt");
query.findInBackground(new FindCallback<MyObject>() {
#Override
public void done(List<MyObject> results, ParseException e) {
....
}
});
I don't have idea how should I transform line:
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
in to working constrain.
Any suggestions?
Try like this. I have tested and it works perfectly. see Relational Queries
ParseQuery<ParseUser> innerQuery = ParseUser.getQuery();
innerQuery.whereEqualTo("userGroupPtr","userGroupPtr"); //value of userGroupPtr
ParseQuery<ParseObject> query = ParseQuery.getQuery(MyObject.class);
query.whereMatchesQuery("createdByPtr", innerQuery);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objectList, ParseException e) {
if(e == null){
}else{
Log.e("Object", e.getMessage());
}
}
});
I know about how to insert in custom table in parse.com database but i don't know about it how to update. i confuse in the how to get all object value from the table. so you have any idea about it.
Thanks in Advances.
You could try to retrieve the object you want to update and then save it edited like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("firstName", "Dan");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
ParseObject person = list.get(0);
person.put("firstName", "Johan");
person.saveInBackground();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
For more examples you could read the chapter for updating objects in Parse.com documentation here https://www.parse.com/docs/android/guide#objects-updating-objects
How do you get a pointer to a parse object?
For example, my ParseUser has a column called "school", which is a pointer to a Parse class "School"
school Pointer<School>
I am able to get a ParseObject of type School, however, I don't know how to get a pointer to this School ParseObject so I can put it in the ParseUser's school field.
I saw similar posts to this one that said to create a parse object without data and give it the correct object ID:
ParseObject mySchoolPtr = ParseObject.createWithoutData("School", mySchoolObject.getObjectId());
currentUser.put("school", mySchoolPtr);
However, after running this code my Parse database for User just lists "(Undefined)" in the school field.
Below is a code snippet capturing this entire process (after the snippet I further explain what is happening in the snippet):
String school = schoolEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
final ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.setPassword(password);
// Query schools and search for name == school, create pointer for that one and put it for user "school"
ParseQuery<ParseObject> query = ParseQuery.getQuery("School");
query.whereEqualTo("name", school);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject school_found, ParseException e) {
if (school_found != null) {
Log.d("School", "Retrieved " + school_found.getString("name"));
ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
currentUser.put("school", myschoolptr);
} else {
Log.d("School", "Error: School name not found in Database. " + e.getMessage());
}
}
});
currentUser.saveInBackground();
showMainActivity();
In my Parse class School, I have a column labeled name.
I have the user input their school, for example "MIT".
I then query the schools class on parse for an equivalent name.
So that gives me the corresponding ParseObject of school class.
I want to put that in the ParseUsers school field.
However, the ParseUser field for school is a pointer to the School class.
How can I get a pointer to my given ParseObject to put into the user's school field?
You have to call currentUser.saveInBackground(); inside the GetCallback for it to update correctly:
...
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject school_found, ParseException e) {
if (school_found != null) {
Log.d("School", "Retrieved " + school_found.getString("name"));
ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
currentUser.put("school", myschoolptr);
currentUser.saveInBackground();
showMainActivity();
} else {
Log.d("School", "Error: School name not found in Database. " + e.getMessage());
}
}
});
I have an Android app that stores company names and those associated with the companies, using Parse.
I am able to get a column created called ownedby that stores the userID.
Now, I want to store information in the company column where the userID in ownedby equals the current user. Here is the query I am using:
ParseQuery<ParseObject> query = ParseQuery.getQuery("midwifefirm");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> midwives, ParseException e) {
// TODO Auto-generated method stub
if(e==null) {
for (ParseObject midwifefirm : midwives) {
String midwiferelation;
ParseUser currentUser;
String userID;
midwiferelation = midwifefirm.getString("ownedby");
currentUser = ParseUser.getCurrentUser();
userID = currentUser.getObjectId();
if (midwiferelation.equals(userID)) {
midwifefirm.put("yearsinpractice", yearsexperience);
midwifefirm.put("practicename", midwifefirmname);
midwifefirm.put("education", education);
}
}
}
else{
Log.d("notretreive", "Error: " + e.getMessage());
}
}
});
This would seem to work; but in the backend, no data is stored, so I guess my "if statement" never works.
Is there anything obvious I am doing wrong? This is for a thesis project, so I can attempt to fake things a bit for the demo, but would like to learn how to do it right.
Thanks so much
Michael
Not sure I got the code below completely right. But the issue you have lies in the fact that ownedBy is a (as I understand) pointer to a User.
When you then try do midwifefirm.getString("ownedby"), you are not getting the objectId as you expect, instead you get something like {'type':'__pointer', 'class' : '_User', 'objectId' : 'xxx'} (I do not remember the notation but hope you get the idea; the literal pointer string value).
Knowing this it is not odd that an objectId xxx String never can equal midwifefirm.getString("ownedby").
Instead of getString, you can call getParseUser, which will return an empty ParseUser with the correct objectId, recall that the pointer only has the minimal information needed to retrieve the object it points to.
You should now be able to make a direct comparison between your current ParseUser and the pointer ParserUser. The fact that the latter is empty should not matter as it only needs the objectId to perform the comparison.
Try this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("midwifefirm");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> midwives, ParseException e) {
// TODO Auto-generated method stub
if(e==null) {
for (ParseObject midwifefirm : midwives) {
ParseUser midwiferelation;
ParseUser currentUser;
midwiferelation = midwifefirm.getParseUser("ownedby");
currentUser = ParseUser.getCurrentUser();
if (midwiferelation.equals(currentUser)) {
midwifefirm.put("yearsinpractice", yearsexperience);
midwifefirm.put("practicename", midwifefirmname);
midwifefirm.put("education", education);
}
}
}
else{
Log.d("notretreive", "Error: " + e.getMessage());
}
}
});