简体   繁体   中英

Passing variables to CloudFunction in ParseSwift correctly

I've been trying to make my static CloudCode function dynamic by passing data when calling the function.

I've been using the template from the ParseSwift Playground to create a CloudCode struct that takes variables, and then run my function.

On the server, I want to assign the passed data to a Push Notification payload. To access the passed data from the CloudFunction, I followed the ParseServer documentation, which had the examples only for the old Parse SDK.

When I run my Cloud function it gets called, but the server sends back an error 141, telling me that 'request' is not defined.

I simply want to pass String data to modify the text in a Push Notification that is sent via CloudCode.

Here is my CloudFunction:

struct CloudFunction: ParseCloud {

    //: Return type of your Cloud Function
    typealias ReturnType = String

    //: These are required by `ParseCloud`
    var functionJobName: String

    //: If your cloud function takes arguments, they can be passed by creating properties:
    //var argument1: [String: Int] = ["test": 5]
    var test1: [String: String]
    var test2: [String: String]
}

When calling the CloudFunction, I do this:

let data1 = ["test1": "abc"]
let data2 = ["test2": "def"]
let pushTest = CloudFunction(functionJobName: "pushsample", test1: data1, test2: data2)
                    
pushTest.runFunction { result in
   switch result {
   case .success(let response):
      print("Response from cloud function: \(response)")
   case .failure(let error):
      assertionFailure("Error calling cloud function: \(error)")
   }
}

And the CloudCode function is:

Parse.Cloud.define('pushsample', async () => {
       
       var data1 = request.params.test1; 
       var data2 = request.params.test2;
       
       Parse.Push.send({
         channels: ["testChannel"],
            data: {
                alert: data1,
                title: data2,
            }
        }, { useMasterKey: true });
      
      return 'pushsample called successfully';
});

I tried passing different parameters (String instead of [String: String] for instance), but that didn't work either

How do I define request inside the CloudCode function, or how do I correctly pass data to the server via a CloudFunction in ParseSwift?

I'm using ParseServer 4.2.0. and ParseSwift 2.5.1 - thanks in advance!

As Davi suggested, adding request allowed me to access my passed data.

The Swift Playground file uses a Dictionary as an example argument for the Cloud Code function. Since this adds an additional step of accessing your data within the CloudCode function via the defined key-value pair, I simplified it to simple String values passed into the function. This also works.

Here are the parts of the code that I changed, for everyone interested:

CloudCode:

Parse.Cloud.define('pushsample', async (request) => {
       
       var data1 = request.params.test1; 
       var data2 = request.params.test2;
       
       Parse.Push.send({
         channels: ["testChannel"],
            data: {
                alert: data1,
                title: data2,
            }
        }, { useMasterKey: true });
      
      return 'pushsample called successfully';
});

CloudFunction:

struct CloudFunction: ParseCloud {

    typealias ReturnType = String

    var functionJobName: String

    var test1: String
    var test2: String
}

Calling the CloudFunction:

let pushTest = CloudFunction(functionJobName: "pushsample", test1: "data1", test2: "data2")
pushTest.runFunction { result in
   switch result {
   case .success(let response):
      print("Response from cloud function: \(response)")
   case .failure(let error):
      assertionFailure("Error calling cloud function: \(error)")
   }
}

Note: In my case, the order of assigning alert before title inside the CloudCode definition on the server is important to make sure the push notification is displayed correctly. Assigning title before alert will cause the push notification to only show your app name with any alert message defined below it.

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