Larry Pickles
Larry Pickles

Reputation: 4646

Parse Cloud Code Defines with Parse 3.0.0

I'm trying to return values from Parse.Cloud.define functions in cloud code using Parse. I'm using Parse 3.0.0 and I can get it to return values from simple cloud code defines but not complex ones.

I'm coding client side iOS in Objective-C.

Here's the cloud code function (I don't care if this is unsafe, I'm not changing it)

Parse.Cloud.define("doStuff", (request) => {
    const query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.username);
    query.first({useMasterKey:true})
    .then((results) => {
        Parse.User.requestPasswordReset(results.get("email"))
        .then(() => {
            return "good work";
        }).catch((error) => {
        });
    })
    .catch((error) =>  {
    });
});

This works just fine, it sends the email to the user as expected by using the User's username field.

In iOS I'm calling it like this:

[PFCloud callFunctionInBackground:@"doStuff" withParameters:@{@"username" : cleanEntryData} block:^(NSString * object, NSError * error) {
 if (!error) {
NSLog(@"success %@", object);
} else {
 NSLog(@"error %@", error);
}
}];

This call works in iOS and the email is successfully sent to the user for password reset. However, here's the problem.

when I call

 NSLog(@"success %@", object);

the value in Xcode debug window is

success (null)

I expect it to be

success good work

When I a simple cloud code define like so:

Parse.Cloud.define("testing", (req) => {
     return "very good";
});

with iOS like so:

 [PFCloud callFunctionInBackground:@"testing" withParameters:@{@"nothing" : @"nothing"} block:^(NSString * object, NSError * error) {
                                    if (!error) {
                                        NSLog(@"success %@", object);

                                    } else {

                                    }
                                }];

then i get the result in Xcode debugger that i'm looking for

success very good

i don't know why the "doStuff" cloud code define is not returning the string "good work" when the function is clearly executing and sending the email as it should. I've read both the Parse 3.0.0 and JS 2.0.0 guides and they aren't very descriptive on how this should work with Parse Cloud defines. I'm not a JS coder, I only code in mobile, so I'm probably doing something stupid. Any help would be great. Thanks.

Upvotes: 0

Views: 697

Answers (1)

David Pasztor
David Pasztor

Reputation: 54716

There's no issue in your iOS code, the issue lies in the cloud code, so you'll need to change the cloud code, since it's not necessarily unsafe, but rather flawed.

The issue is that you are nesting Promises inside each other instead of chaining them together, hence the single nested return value is lost in the several nested layers.

Parse.Cloud.define("doStuff", (request) => {
    const query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.username);
    return query.first({useMasterKey:true})
    .then((results) => {
        return Parse.User.requestPasswordReset(results.get("email"));
    }).then(() => {
        return "good work";
    })
});

Upvotes: 1

Related Questions