In Parse I have another column in the User table known as 'Friends' which is an array. From my current user I am trying to add to another user's friends array.
I have tried:
//buddy = a string of the other person's username
//to add to CURRENT user's array (this is working)
UserList.user.addUnique("Friends", buddy);
UserList.user.saveEventually();
//this is to add to another user's array (NOT WORKING)
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
for (ParseObject friend : objects) {
friend.addUnique("Friends",UserList.user.getUsername());
friend.saveEventually();
}
} else {
// Something went wrong.
Log.d("Username", "Error: " + e.getMessage());
}
}
});
Write a method in a java that passes data to the cloud, and calls the function on the cloud by its name:
// the passes to the cloud is stored on HashMap
HashMap<String, Object> params = new HashMap<String, Object>();
// user's name to add the buddy to it
params.put("username", username);
// buddy's name to pass to the cloud
params.put("buddy", buddy);
ParseCloud.callFunctionInBackground("addBuddyToFriendsList", params, new FunctionCallback<String>(){
public void done(String result, ParseException e){
// e==null means no errors in running the function
if(e==null){
System.out.println(result);
}else{
System.out.println(e.getMessage());
}
}
});
Ok, and now let's write the function to be used in the cloud (assuming you know how to deploy functions on the cloud):
[EDITED]
Parse.Cloud.define("addBuddyToFriendsList", function (request, response){
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.find({
success: function(user) {
// query.find returns an array.....
user[0].addUnique("Friends", request.params.buddy);
user[0].save(null, {
success: function(success){
response.success();
},
error: function (error){
response.error(error);
}
});
},
error: function(object, error) {}
});
});
Try this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// The query was successful.
for (ParseObject friend : objects) {
friend.addUnique("Friends",UserList.user.getUsername());
friend.saveEventually();
}
} else {
// Something went wrong.
Log.d("Username", "Error: " + e.getMessage());
}
}
});
I tried to call that class in different way then you did, I referred to it like a normal class. Usually this way works for me.
The thing is with "ParseUser" type is limited, for security reasons.
Related
I use parse AnyPhone for Login using mobile number and after successfully login (OTP verification), i want to get all parse user from User table for that i used bellow query but i only get Login user Data not all. Thanks in Advance.
ArrayList<String> Email = new ArrayList<String>();
Email.add("test2#gmail.com");
Email.add("test#g.com");
Email.add("test#gmail.com");
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereContainedIn("email", Email);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if(e==null){
Log.i("Size",""+objects.size());
}
}
});
void getAllParseUser() {
ParseUser user = ParseUser.getCurrentUser();
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo(ParseTableConstants.OBJECT_ID, user.getObjectId());
progressBar.showProgress();
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
insertData(objects);
} else {
// Something went wrong.
progressBar.hideProgress();
}
}
});
}
This code will give you all parse user
I solved My Problem by setting ACL in javascript File.Now i am getting all the Users.
var acl = new Parse.ACL();
// public can read
acl.setPublicReadAccess( true );
// public cannot write
acl.setPublicWriteAccess( false );
// user can read data
acl.setReadAccess( user.id, true );
// user can write data
acl.setWriteAccess( user.id, false );
user.setACL(acl);
user.save();
I've searched everywhere but haven't found anything about how to check if a username/email address in the parse database is taken. I've found some answers on the internet on parse.com forums but they weren't clear.
Thanks if you could help.
If this has an answer somewhere, then please comment instead of marking so I can delete it.
I think this will do what you need if I understand your question correctly:
final ParseQuery<ParseUser> emailQuery = ParseUser.getQuery();
emailQuery.whereEqualTo("email", emailAddress);
final ParseQuery<ParseUser> usernameQuery = ParseUser.getQuery();
usernameQuery.whereEqualTo("email", username);
List<ParseQuery> queries = new ArrayList<>();
queries.add(emailQuery);
queries.add(usernameQuery);
final ParseQuery<ParseUser> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> results, ParseException e) {
// results has the list of users that match either the email address or username
}
});
https://www.parse.com/docs/android/guide#queries-compound-queries
Or you could do it this way:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// yippee!!
} else {
switch (e.getCode()) {
case ParseException.USERNAME_TAKEN: {
// report error
break;
}
case ParseException.EMAIL_TAKEN: {
// report error
break;
}
default: {
// Something else went wrong
}
}
}
}
});
Try this to check your data in parse. I hope this will helpful for you.
// this is create query of your parse table
ParseQuery<ParseObject> query = new ParseQuery<>(your prars tableName);
query.whereEqualTo(YourParseColumeNameEmail, yourEmail);
query.whereEqualTo(YourParseColumeNamePassword, yourPassword);
// this is for doing in background
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
if (scoreList.size() == 0) {
// if there is no data like your email and password then it,s come here
} else {
// if there is data like your email and password then it,s come here
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
For my Android application, I want to make possible that a user follows another user. So I added a relation column for the users which will contains the following users.
Here is my parse cloud function. I'm just trying to add the currentUser to the followers relation of the user he wants to follow.
Parse.Cloud.define('sabonner', function(request, response) {
var userId = request.params.userId;
var User = Parse.Object.extend('_User');
user = new User({ objectId: userId });
var currentUser = request.user;
var relation = user.relation("abonnesRelation");
relation.add(currentUser);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
Here is how I call it:
String targetId=mUsers.get(position).getObjectId();
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("userId",targetId );
ParseCloud.callFunctionInBackground("sabonner", params,
new FunctionCallback<String>() {
public void done(String result, ParseException e) {
if (e == null) {
Log.d("ParseCloud", "successs :"+result);
} else {
Log.d("ParseCloud", "unsuccesss :" + e.getMessage());
}
}
});
For now I'm having this error:
parsecloud java.lang.ClassCastException: com.parse.ParseUser cannot be cast to String.
It's my first use of Parse Cloud so I'm sure that there there are many things to modify here.
Seems like in functionCallback you're receiving ParseUser not String.
Just try to change your callback as follows:
ParseCloud.callFunctionInBackground("sabonner", params, new FunctionCallback<ParseUser>() {
public void done(ParseUser result, ParseException e) {
if (e == null) {
Log.d("ParseCloud", "successs :"+result);
} else {
Log.d("ParseCloud", "unsuccesss :" + e.getMessage());
}
}
});
I am using ParseUser for user management. Having the username (unique), I planned to query the User class to identify the user and then put a new score to the "score" field, just like a Leaderboard. Coded as follows:
Code:
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseObject object, ParseException e)
{
if (e == null)
{
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
object.put("score", new_score);
object.saveInBackground();
onBackPressed();
}
});
}
}
});
}
Situation:
It states that The type new FindCallback(){} must implement the inherited abstract method FindCallback.done(List, ParseException) . I have tried that FindCallback.done and found it unsucessful.
Question:
I would like to ask how the other fields of the user be updated with the username given? Thanks!
As per the parse documentation said,
The ParseUser class is secured by default. Data stored in a ParseUser can only be modified by that user. By default, the data can still be read by any client. Thus, some ParseUser objects are authenticated and can be modified, whereas others are read-only.
Specifically, you are not able to invoke any of the save or delete type methods unless the ParseUser was obtained using an authenticated method, like logIn or signUp. This ensures that only the user can alter their own data.
The following illustrates this security policy:
ParseUser user = ParseUser.logIn("my_username", "my_password");
user.setUsername("my_new_username"); // attempt to change username
user.saveInBackground(); // This succeeds, since the user was authenticated on the device
// Get the user from a non-authenticated manner
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(user.getObjectId(), new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
object.setUsername("another_username");
// This will throw an exception, since the ParseUser is not authenticated
object.saveInBackground();
}
});
please go through this link Reference
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseUser object, ParseException e)
{
if (e == null)
{
object.put("score", new_score);
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
if(e==null)
onBackPressed();
}
});
}
}
});
}
As far as I understand from your question is; you want to find the user entry in user table via using the specified user id and update the score field. First suggestion, instead of findInBackground use getFirstInBackground. Following that check the returned user object if not null perfrom the user update operation. Then call saveInBackground. saveInBackground is used for perfroming actions when the save is succesful. In your code, I see that you try to save the object in done method again. Also, one suggestion is that control the ACL.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// No error e is null
// check user object
if (user == null) {
// no user , error
} else {
// perfrom save operation
// put score field
// call save in background
// in done method update UI for example.
}
} else {
// Something went wrong.
}
}
});
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.