I'm trying to set the email in a user obtained from Facebook SDK. If I set the email with any string like this:
user.setEmail("whatever#whwhwh.com");
user.saveInBackground();
It works, no problem... it appears in Parse's Data Browser...
If I try to do what I need to do(see code below), then it doesn't work, I've checked the String value and it's ok, it is the needed email....I've tried different things.. no luck.. Please help, thanks.
user.setEmail(user.getProperty("email").toString());
user.saveInBackground();
Make sure the email being supplied is not used by any other user. This is because an email must be unique.
When calling saveInBackground, it will actually not throw any exception if one does occur. So in your case, if the email is a duplicate, the email won't get saved and no exception is thrown.
To catch an exception with saveInBackground, you can use the SaveCallback.
user.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// No exception
} else {
// Exception occured
}
}
});
For a list of exceptions you can refer to ParseException.
Related
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
I'm trying to delete a parse object with the following method:
object.deleteInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
if(e !=null) {
Log.e("Parse: ",e.toString());
}
}
});
Console Output
E/Parse:: com.parse.ParseRequest$ParseRequestException: Object not
found.
My object's ACL is public read and owner read & write and I am connected to the owner account.
The strange thing is that I actually can delete the object if it has public write ACL, but I only want the owner have the possibility to delete it.
Has anyone encountered similar issues ?
Okay, I've figured out that I did something else wrong with my user login which caused this error. Anyways thanks to anybody who tried to help me
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
I have project where I send messages between 2 and 2 ParseUsers, and save the messages as a ParseObjects where I keep a Pointer to the 2 users sending messages to each other. The code works for every user in the database except when I send a message to 1 particular user. Then I get "Cannot save a ParseUser that is not authenticated", so it seems like Parse tries to save the other User object, together with saving my message ParseObject, just with this one particular user.
Tried to create a "Save without data" object, but get the same error. Anyone experienced this before?
ParseObject message = new ParseObject("MSG");
message.put("sender", meUser);
message.put("receiver", otherUser);
message.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if( e== null){
Log.i(TAG, " Worked...");
}else {
e.printStackTrace();
}
}
});
The problem was that I actually did try to to change another users object. I found out where by backtracking using the "isDirty()" method on the object that was giving me issues to find out where I was making changes on the object. This method lead me to the root of the problem.
isDirty() returns true if you have made any changes to an object but not saved it yet.
Hi I read the tutorials on basic parse features, etc. and I am using the ParseUser feature. From the tutorial this is shown :
ParseUser.logInInBackground("Jerry", "showmethemoney", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
} else {
// Signup failed. Look at the ParseException to see what happened.
}
}
});
I was wondering how I could look at the ParseException to see what happened. For example, I want to inform the user that a bad username was entered, or a bad password.
Thank you
a Pretty easy way to resolve this is to use
e.getMessage();
// this probably shows you appropriate message
// or you can use
e.getCode();
// and match code with the integers given in Parse Doucmentation
// for example.
// e.EMAIL_NOT_FOUND is code for when email is not found in registered users class
if you need see the exception, can try show in the log or where you decide, in your code for te show in the log where Log.d(nameofalert,mensaje), only need see the logcat for the identify your exception
ParseUser.logInInBackground("Jerry", "showmethemoney", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.d("user not null",e.toString());
} else {
Log.d("error",e.toString());
// Signup failed. Look at the ParseException to see what happened.
}
}
});
Place your code in between try catch and in the catch block try to handle stuff that will go when user enter bad username or password
try{
}
catch(ParseException e)
{
// do stuff here
}
how about just doing e.printStackTrace(), that will print all the information that you need pertaining to the exception