Search code examples
javascriptnode.jsparse-platformparse-serverparse-cloud-code

Cloud Code 'Invalid Function' when updating user object


I am trying to update a user object, that is not the current user via useMasterKey. However, I get the error "invalid function" when running it. "blockedFrom" is an array in the user object that stores the list of users who blocked the concerned user and I am trying to add the usernames via addUniqueObject.

Parse.Cloud.job('addBlockedFrom', function(request, status) {
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.otherUser);  
    query.each(function(record) {
        record.addUniqueObject("blockedFrom", request.params.username);
        return record.save({useMasterKey:true});
    },{useMasterKey:true}).then(function(result) {
        console.log("addBlockedFrom completed.");
        status.success("addBlockedFrom completed.");
    }, function(error) {
        console.log("Error in addBlockedFrom: " + error.code + " " + error.message);
        status.error("Error in addBlockedFrom: " + error.code + " " + error.message);
    });
});

Solution

  • After replacing Parse.Cloud.job with Parse.Cloud.define as suggested by @TanzimChowdhury, I still had the invalid function error because of status.success and status.error. Status was undefined, replacing status with response fixed it.

    Working Code

    Parse.Cloud.define("addBlockedFrom", function(request, response) { 
    var query = new Parse.Query(Parse.User);    
    query.equalTo("username", request.params.otherUser);                                                                                     
    query.each(function(user) {
          user.addUnique("blockedFrom", request.params.username);
          return user.save( null, { useMasterKey: true });
      }).then(function() {
        // Set the job's success status
        response.success("addBlockedFrom successfully.");
      }, function(error) {
        // Set the job's error status
        response.error("Error addBlockedFrom.");
      });
    });