简体   繁体   中英

Edit a User with Cloud Code javascript and iOS Parse SDK

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?

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

似乎您没有在服务器上重新加载main.js(用于编写云代码函数的文件)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM