0

I am starting to make use of a Cloud-Function in an iOS/Parse-server app.

Here is the swift code, on the client app:

PFCloud.callFunction(inBackground: "myCloudFunction",
                   withParameters: ["unitID": theObject.objectID,
                                    "userID": PFUser.current()!.objectId!]) {
    [weak self] (object: Any?, error: Error?) in
    if error != nil {
        if error?.localizedDescription == "NOTHING"
        {print("The given object has not been found. This should not happen.")}
        else {
            print("Error in \(#function)\n" + (error?.localizedDescription)!)
            print("object = \(String(describing: object))")
        }
        return
    }

    .........
}

The issue is, there seems to be a problem inside this function:

+ (NSMutableURLRequest *)urlRequestWithURL:(NSURL *)url
                                httpMethod:(NSString *)httpMethod
                               httpHeaders:(NSDictionary *)httpHeaders
                                parameters:(NSDictionary *)parameters {
                              .......
                                }

On this instruction:

    request.HTTPBody = [NSJSONSerialization
    dataWithJSONObject:parameters
               options:(NSJSONWritingOptions)0
                 error:&error];

I have no idea what the problem is, but I presume I am using some kind of wrong syntax in the code:

PFCloud.callFunction(inBackground: "myCloudFunction",
                   withParameters: ["unitID": theObject.objectID,
                                    "userID": PFUser.current()!.objectId!]) ...

Any relevant tip would be very much appreciated.

Just in case it may be usful, here is the parse cloud function:

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

  var theQuery;
  theQuery = new Parse.Query("TheCoud_List");
  theQuery.equalTo("objectId", request.params.unitID);
  theQuery.equalTo("owner", request.params.userID);

  theQuery.find().then
  (function(resUnit) {
    // If there is no UNIT we return an error.
    if (!resUnit.length) response.error("NO-UNIT");
    else response.success('ALL-OK');
  });
});

And this is what I can see in the debubbing window on the iOS side:

2020-02-25 ... MyApp[13018:2024085] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_NSCoreDataTaggedObjectID)'
*** First throw call stack:
(0x19ba47180 0x19ac1f9f8 ... 0x19b668cd4)
libc++abi.dylib: terminating with uncaught exception of type NSException
Michel
  • 10,303
  • 17
  • 82
  • 179
  • Could you share your cloud code function code and also the error that you have in your iOS app when calling this function? – Davi Macêdo Feb 25 '20 at 00:38
  • I added the code for the cloud function in the post, but since the it is not even called I doubt this will be of any use. – Michel Feb 25 '20 at 01:11
  • I have also added what I can see from the side of the iOS app. – Michel Feb 25 '20 at 01:22
  • Sorry for the delay. What is the parse version you are using? If >= 3.0, your cloud code function should use async/await style: https://github.com/parse-community/parse-server/blob/master/3.0.0.md#migrating-functions – Davi Macêdo Feb 28 '20 at 20:08
  • Yes indeed, I ended up by making it work using a quite different syntax. Following this guide: https://docs.parseplatform.org/cloudcode/guide/. Thank you. – Michel Feb 29 '20 at 01:32

0 Answers0