0

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);
    });
});
1
  • Add the full error. So we can see what type of error you get Commented Jun 11, 2020 at 5:59

3 Answers 3

1

The error is right , it's not a valid cloud code function. What you defined above is a Job. You need to define a cloud code function.

Replace

"Parse.Cloud.job" with "Parse.Cloud.define"

That should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Thank you!
1

If you have parse server version >3 then your cloud code must be changed to new version.

Try this:

Parse.Cloud.define("addBlockedFrom", async (request) => {

  var query = new Parse.Query(Parse.User);    
  query.equalTo("username", request.params.otherUser); 
  try{
    var otherUser = await query.first();
    otherUser.addUnique("blockedFrom", request.params.username);
    return otherUser.save( null,{useMasterKey:true});
  }catch(err){
    throw err;
  }                                                                                    

});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.