繁体   English   中英

正确地将变量传递给 ParseSwift 中的 CloudFunction

[英]Passing variables to CloudFunction in ParseSwift correctly

我一直在尝试通过在调用 function 时传递数据来使我的 static CloudCode function 动态化。

我一直在使用 ParseSwift Playground 中的模板来创建一个接受变量的 CloudCode 结构,然后运行我的 function。

在服务器上,我想将传递的数据分配给推送通知有效负载。 为了从 CloudFunction 访问传递的数据,我遵循了 ParseServer 文档,该文档仅包含旧 Parse SDK 的示例。

当我运行我的 Cloud function 时,它会被调用,但服务器会发回错误 141,告诉我未定义“请求”。

我只是想传递字符串数据来修改通过 CloudCode 发送的推送通知中的文本。

这是我的 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]
}

调用 CloudFunction 时,我这样做:

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

CloudCode function 是:

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';
});

我尝试传递不同的参数(例如 String 而不是 [String: String]),但这也不起作用

如何在 CloudCode function 中定义request ,或者如何通过 ParseSwift 中的 CloudFunction 正确地将数据传递到服务器?

我正在使用 ParseServer 4.2.0。 和 ParseSwift 2.5.1 - 在此先感谢!

正如 Davi 建议的那样,添加request允许我访问我传递的数据。

Swift Playground 文件使用字典作为云代码 function 的示例参数。 由于这增加了一个额外的步骤,即通过定义的键值对访问 CloudCode function 中的数据,因此我将其简化为传递给 function 的简单字符串值。 这也有效。

以下是我更改的代码部分,供所有感兴趣的人参考:

云代码:

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';
});

云功能:

struct CloudFunction: ParseCloud {

    typealias ReturnType = String

    var functionJobName: String

    var test1: String
    var test2: String
}

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

注意:在我的例子中,在服务器上的 CloudCode 定义中,在title之前分配alert的顺序对于确保正确显示推送通知很重要。 alert之前分配title将导致推送通知仅显示您的应用名称以及在其下方定义的任何alert消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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