0

I'm developing an iOS app with Parse SDK hosted on back4app, my app in the back4app dashboard hosts a main.js file in Cloud Code that sends push notifications, it gets called by code and it works fine.

Now I've added a blockuser.js file in my Cloud Code, such file should edit the isBlocked column (of type Boolean) of a specific user in _User class and set it to true, here's the code I use:

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

    var userId = request.params.userId,

    var User = Parse.Object.extend('_User'),
    user = new User({ objectId: userId });

    user.set('isBlocked', true);

    Parse.Cloud.useMasterKey();
    user.save().then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });

});

Here's the Swift code I wrote to call that function:

let request = ["userId" : userPointer.objectId!] as [String : Any]

PFCloud.callFunction(inBackground: "blockUser", withParameters: request as [String : Any], block: { (results, error) in
        if error == nil {
           print ("\(userPointer["username"]!) has been blocked!")
           // error in cloud code
        } else {
            print ("\(error!.localizedDescription)")
}})

The Xcode console prints out this message:

[Error]: Invalid function. (Code: 141, Version: 1.14.2)

In fact, that blockUser function doesn't work at all.

Anybody knows what I'm doing wrong in the .js or swift code?

2 Answers 2

1

After a few attempts, I've figured it out, here's the function I've added in my main.js file in Cloud Code:

// BLOCK A USER  ----------------------------------------
Parse.Cloud.define("blockUser", function(request, response) {

    var userId = request.params.userId;

    var User = Parse.Object.extend('_User'),
    user = new User({ objectId: userId });

    user.set('isBlocked', true);

    Parse.Cloud.useMasterKey();
    user.save(null, { useMasterKey: true } ).then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });
});

And here's the Swift 3 code to call blockUser function:

    let request = [
        "userId" : userPointer.objectId!
    ] as [String : Any]

    PFCloud.callFunction(inBackground: "blockUser", withParameters: request as [String : Any], block: { (results, error) in
     if error == nil {
          print ("\(userPointer["username"]!) has been blocked!")
     // error
     } else {
         print ("\(error!.localizedDescription)")
   }})
Sign up to request clarification or add additional context in comments.

Comments

0

it seems like you did not reload your main.js (file where you write cloud code functions) on server

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.