How to update values in users table of Parse.com - android

Guys I want to update the rows of users table in Parse.com. So, far I have used user.SignupinBackground method to update password and other fields(added by me other than username and password) but it throws an exception "Cannot sign up a user that has already signed up."
Is there a way to update the data in the rows of users table of parse.
I have already visited this link but i didn't found any method for updating the data in the rows of the current user.

You can achieve by this code
ParseUser parseUser = ParseUser.getCurrentUser();
parseUser.put("columnName", username);
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
// TODO Auto-generated method stub
if (e != null){
e.printStackTrace();
}else{
//updated successfully
}
}
});

Related

Saving a user with an already exist email or username causes ParseUser to return wrong value

I have the following code that allows users to update their data:
String username = "Any username that is currently already used by another user";
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
currentUser.setUsername(username);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// All good!
} else {
// Error: Account already exists for this username.
Log.e("Error: ", e.getMessage());
}
}
}
});
When the user is trying to update the username with one that is already used by someone else, it throws an error:
Account already exists for this username.
Which is exactly what I'd want it to, but when the user goes back (without making another request to change the username to one that is available) the ParseUser.get("username") returns the value that wasn't saved because it already exists (instead of the real value stored at the moment in the server).
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.fetchInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException exception) {
if (exception == null) {
// username now returns ("Any username that is currently already used by another user").
String username = user.get("username").toString().trim();
} else {
// Error
}
}
});
The only way I've found so far to fix the issue is by uninstalling/reinstalling the app. What's causing this? And is there any good way to fix this?
You should backup the current username before setUsername(newName).
if exception happened you have two options either :
Re fetch the user object by ParseUser.getCurrentUser().fetch() (to restore the object to his previous state)
Re set the username to the previous value(that you already back up before)
Why this happened ?
because when you called the setUsername() of the user object you changed your local copy of the object (but you didn't sync it yet with the server) and then when you made save() and the save operation failed the local copy still has the latest changes that you made locally

"Cannot save a ParseUser that is not authenticated" with authenticated user

I'm using Parse as my backend and I'm trying to "like" a post that another user posted on the app. I'm querying to get the post, then incrementing the number of likes by 1, then adding the current user's object ID to an array that holds all the ID's of users which liked the post.
carLikeQuery.getInBackground(carItem.getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
object.increment("likes");
object.addUnique("usersWhoLike", ParseUser.getCurrentUser().getObjectId());
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e==null) {
Log.d("SAVE", "Like saved :)");
} else {
Log.e("SAVE", "Not saved :( :" + e.getLocalizedMessage());
}
}
});
}
});
The error I'm getting:
E/SAVE: Not saved :( :java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
I saw the source code for the ParseUser from somewhere:
void validateSave() {
if (getObjectId() == null) {
throw new IllegalArgumentException("Cannot save a ParseUser until it has been signed up. Call signUp first.");
}
if (!isAuthenticated() && isDirty()) {
throw new IllegalArgumentException("Cannot save a ParseUser that is not authenticated.");
}
}
Doing the same kind of checking in my code reveals that the the currentUser is AUTHENTICATED and NOT DIRTY.
What could the issue be? To be honest, I want to say that it was working just fine before today, but obviously I was changing something and made a mistake down the line and I can't find it! Any help would be greatly appreciated.
I resolved the issue by creating an entirely new Parse application with the same data structure/layout. It just plain worked without any code changes.
You can follow the issue on GitHub here

Can I update another ParseUser data different by the on logged in?

I want to update a field from table 'User' of a ParseUser different by the one logged in.
The code below is not working and I was wondering if there is some impediment that does not allow to update other's user info.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereMatches("objectId", "4mdsJKKgDJ");
userQuery.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser parseUser, ParseException e) {
// I retreive the parseUser object correctly here
int score = parseUser.getInt("score"); // I have added this column in User table with a default value of 0
parseUser.put("score", score+1);
parseUser.saveInBackground(); // nothing happens. the value is not updated
}
});
Thanks.
I answer myself to my question: you cannot update another user info, different by the one logged in.
Using:
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("ParseException: "+ e);
}
});
revealed the problem:
com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
So I will need to use another table to keep this info.

Problem with ParseUser.logout() before fetch

I'm using parse.com Android sdk, in the parseUser I have added a column pointer to other data class named pointer. I access this data by:
ParseUser user=ParseUser.getCurrentUser();
ParseObject pointer= user.getParseObject("pointer");
pointer.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
}
}
But I have a problem when I try to logout before doing this fetch. I logout by:
ParseUser.logOut();
and when this function is invoked the the app get locked.
You simply need to wrap this call to fetch in a check to see if there is still a logged in user:
ParseUser user = ParseUser.getCurrentUser();
if (user != null) {
// rest of your code here, as we have a currently logged in user
}

Custom column parse query

I am using Parse as back-end for my Android Application.I have created two custom columns in the User Class.Now I want my application user to signup with my application.I am unable to store user data for the custom columns.How do i do that?
Directly from the Parse Docs:
ParseUser user = new ParseUser();
user.setUsername("my name");
user.setPassword("my pass");
user.setEmail("email#example.com");
// other fields can be set just like with ParseObject
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
I suggest you read the Parse docs very thoroughly - most anything you will want to do with the service will be somewhere in the docs, they are very good.

Categories

Resources