使用修改的键/值重新保存PFUsers

时间:2014-11-04 22:07:35

标签: javascript parse-platform

我正在尝试使用Javascript Parse SDK编写云功能,我想知道是否可以为各种用户重新保存行密钥。到目前为止,我已经得到它来查询我想要的用户,现在我只是想把这些用户的maxAwardedPosts'重新设置为0.我以为我必须以某种方式使用for循环而我假设重新保存每个人?

Parse.Cloud.define("resetUserData", function(request, response) {

 // Cycle through User Class
 // Find all Users that have 'maxAwardedPosts' > 0
 var query = new Parse.Query(Parse.User);
 query.greaterThan("maxAwardedPosts", 0);
 query.find({
   success: function(users) {
     // Reset these users 'maxAwardedPosts' key back to 0 somehow?
   }
 });


});

1 个答案:

答案 0 :(得分:1)

如果您想为迁移执行此操作,可以使用background job

Parse.Cloud.job("userMigration", function(request, status) {
  // Use master key because user objects may have ACLs that prevent from modifying
  Parse.Cloud.useMasterKey();

  // Query for users having maxAwardedPosts greater than 0
  var query = new Parse.Query(Parse.User);
  query.greaterThan("maxAwardedPosts", 0);
  query.each(function(user) {
      user.set("maxAwardedPosts", 0);
      return user.save();
  }).then(function() {
    // Set the job's success status
    status.success("Migration completed successfully.");
  }, function(error) {
    // Set the job's error status
    status.error("Uh oh, something went wrong.");
  });
});

或者在云端功能中使用相同的代码。