user3239866
user3239866

Reputation: 66

Update user object from Parse cloud code is not saving

Currently I am trying to update the username and password from parse cloud code, but In the parse.com console I am seeing the success messages, but the object is not actually saved in the parse.com database. Here is the contents of cloud/main.js

// code to update username
Parse.Cloud.define("updateUserName", function(request, response){
if(!request.user){
    response.error("Must be signed in to update the user");
    return;
}
Parse.Cloud.useMasterKey();
var userId = request.params.id;
var userName = request.params.userName; 
// var User = Parse.Object.extend("User");
var updateQuery =  new Parse.Query(Parse.User); 
updateQuery.get(userId,{
    success: function(userRecord){
        console.log(userRecord.get("id"));
        userRecord.set("username", userName);
        // userRecord.set("resetToken", "Apple");
        userRecord.save(null,{
            success: function(successData){                 
                response.success("username updated successfully.");
                // userRecord.fetch();                  
            },
            error: function(errorData){
                console.log("Error while updating the username: ",errorData);

            }
        });

    },
    error: function(errorData){
        console.log("Error: ",errorData);
    }
});
});

Parse.Cloud.define("resetPassword", function(request, response){
var successMsg = "";
if(!request.user){
    response.error("Must be signed in to update the user");
    return;
}
Parse.Cloud.useMasterKey();
var resetToken = request.params.resetToken;
var password = request.params.password; 
// var User = Parse.Object.extend("User");
var updateQuery =  new Parse.Query(Parse.User);
// updateQuery.equalTo("resetToken", resetToken);
updateQuery.get(resetToken,{
    success: function(userRecord){
        // console.log(userRecord.get("id"));
        // userRecord.set("password",password)
        userRecord.set("password",password);          
        userRecord.save(null, {
            success: function(successData){
                successMsg = "Password Changed !";
                console.log("Password changed!");
                userRecord.set("resetToken", "");
                userRecord.save();
            },
            error: function(errorData){
                response.error("Uh oh, something went wrong");
            }
        })


    },
    error: function(errorData){
        console.log("Error: ",errorData);
    }
});
response.success(successMsg);
});

The code actually runs without any error, but it is not updating the values in the database. Here is how I am calling these cloud functions in js/index.js

$(".update-user").click(function(){
   Parse.Cloud.run("updateUserName", {id: $(this).data("id"), username:   $(".uname").val()},
       {
         success: function(successData){
         console.log("username updated successfully.");
         $("#editModal").modal("hide");
         $(".edit-modal").hide();
       },
       error: function(errorData){
       }
   });
});

The contents that I see in the firefox console username updated successfully. The contents that I see in parse.com console

I2015-12-11T06:15:13.361Z]v106 Ran cloud function updateUserName for user chfgGhaPEl with:
Input: {"id":"MAvm9FlGgg","username":"testuser12"}
Result: username updated successfully.

But this line of code userRecord.set("resetToken", "Apple"); is updating the resetToken column in the database, but why not it is not letting me update the username/password(or other columns that I didn't try updating) columns ?

Upvotes: 1

Views: 2293

Answers (3)

SharK
SharK

Reputation: 2215

Parse doesn't support HTTP PUT requests to save the data. Hence we need to use PUSH request containing a method call to save the data.

However it has to have a the line, Parse.Cloud.useMasterKey(); before calling the save method.

Upvotes: 0

user3239866
user3239866

Reputation: 66

What worked for me is

//cloud/main.js
Parse.Cloud.define("updateUserName", function(request, response){
if(!request.user){
    response.error("Must be signed in to update the user");
    return;
}
if(request.params.IsAdmin == false){
    response.error("Only the administrators can edit username.");
    return; 
}
Parse.Cloud.useMasterKey();
// var userId = request.params.Id; --> I guess he was the main culprit,  the params and the actual column value should match. Instead of passing Id from my client code(see below) I just passed objectId and it worked.
var userId = request.params.objectId;
// var userName = request.params.username;  
var name = request.params.name;
// var User = Parse.Object.extend("User");
var updateQuery =  new Parse.Query(Parse.User); 
console.log("id from params: "+userId);
updateQuery.equalTo("objectId", userId);
updateQuery.first({
    success: function(userRecord){          
        // userRecord.set("username", userName);
        userRecord.set("name", name);           
        userRecord.save(null,{
            success: function(successData){                 
                response.success("username updated successfully.");
                userRecord.fetch();                 
            },
            error: function(errorData){
                console.log("Error while updating the username: ",errorData);

            }
        });         

    },
    error: function(errorData){
        console.log("Error: ",errorData);
        response.error(errorData);
    }
});
});

// js/index.js
/* initially I was using Id instead of objectId, the field names are case
sensitive
Parse.Cloud.run("updateUserName", {id: $(this).data("id"), name:   $(".name").val(),IsAdmin: Parse.User.current().get("IsAdmin") },*/
Parse.Cloud.run("updateUserName", {objectId: $(this).data("id"), name: $(".name").val(),IsAdmin: Parse.User.current().get("IsAdmin") },
        {
            success: function(successData){
                console.log("username updated successfully.");
                $("#editModal").modal("hide");
                $(".edit-user-server-error").html("");
                $(".edit-modal").hide();
                location.reload();                    
            },
            error: function(errorData){
                console.log(errorData);
                $(".edit-user-server-error").html("");
                $(".edit-user-server-error").html(errorData.message);

            }
        });

So I had referred to this post and it gave me an hint, based on that I just did one trial and error thing and got it working.

You can pass any sort of values in the parameters to this Cloud Function, so you might want to specify exactly which properties you wish to update in this manner. Also, don't forget to actually validate that request.user is allowed to perform such an operation.

Upvotes: 0

kingspeech
kingspeech

Reputation: 1836

I analyse your code. Instead of using get to retrieve correlated user, use first where you specify the user id as query constraint. One example (working) code is below where user information is updated in Parse User table. Hope this helps.

Regards.

Parse.Cloud.define("updateUser", function(request, response) 
{
  Parse.Cloud.useMasterKey();
  var query = new Parse.Query(Parse.User);
  var objectId = request.params.objectId;
  var username = request.params.username;
  var email = request.params.email;
  var userType = request.params.userType;
  var password = request.params.password;

  query.equalTo("objectId", objectId);
  query.first({
      success: function(object) 
      {
        object.set("username", username);
        object.set("email", email);
        object.set("userType", userType);
        object.set("password", password);
        object.save();
        response.success("Success");
      },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
      response.error("Error");
    }
  });
}); 

Upvotes: 3

Related Questions