从 Parse 云代码更新用户对象未保存

Update user object from Parse cloud code is not saving

目前我正在尝试通过解析云代码更新用户名和密码,但在 parse.com 控制台中我看到了成功消息,但对象实际上并未保存在 parse.com数据库。这里是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);
});

代码实际运行没有任何错误,但它没有更新数据库中的值。这是我在 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){
       }
   });
});

我在firefox控制台看到的内容username updated successfully. 我在 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.

但是这行代码 userRecord.set("resetToken", "Apple"); 正在更新数据库中的 resetToken 列,但为什么它不让我更新 username/password(或我没有尝试更新的其他列) 列 ?

我分析你的代码。不要使用 get 来检索相关用户,而是首先使用您将用户 ID 指定为查询约束的地方。下面是一个示例(工作)代码,其中在 Parse User table 中更新了用户信息。希望这有帮助。

此致。

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");
    }
  });
}); 

对我有用的是

//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);

            }
        });

所以我参考了这个 post 它给了我一个提示,基于此我只是做了一个试错的事情并让它工作。

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.

Parse 不支持 HTTP PUT 请求来保存数据。因此我们需要使用包含方法调用的 PUSH 请求来保存数据。

但是它必须有一行,Parse.Cloud.useMasterKey(); 在调用保存方法之前。