gonso
gonso

Reputation: 2085

iOS & Parse.com: How to set User when calling Parse Cloud Code?

I have defined a function in Parse.com's Cloud Code. According to documentation the request.usr should be "The Parse.User that is making the request. This will not be set if there was no logged-in user.".

Im invoking my method from iOS and the user object is "empty", as in all attributes are undefined.

I guess Im missing how to set it when invoking the cloud code.

Here are my pieces:

1) cloud code

Parse.Cloud.define("userRatesMovie", function(request, response) {
  //Parameters: user, movieId, rating
  var user = request.user;
  var username = "nothing"
  var movieId = request.params.movieId;
  var rating = request.params.rating;

  var aux = "User: " + username + " Rated: " + movieId + " with a " + rating + " user " + user.username +":" +user.objectId;
  console.log(aux);

  //Fetch for previous rating for this user,movie pair
  response.success(aux);
});

For now Im just writing and returning a log line.

2) iOS code

    //Test code
PFUser *pfUser = [PFUser currentUser];
NSLog(@"User Id:%@ Username:%@",pfUser.objectId, pfUser.username);
[PFCloud callFunctionInBackground:@"userRatesMovie"
                   withParameters:@{
                                    @"movieId":@"12345",
                                    @"rating":@"3"}
                            block:^(NSString *someString, NSError *error) {
                                if (!error) {
                                    NSLog(@"userRatesMovie:%@",someString);
                                }
                            }];

I first check that I have a PFUser and then invoke my cloud code

3) Output

    User Id:32P3Hwk1Hv Username:1NpFRFVPFJT45znXosx8g8Ay2
    userRatesMovie:User: nothing Rated: 12345 with a 3 user undefined:undefined

As you can see user.username and user.objectId are undefined.

Upvotes: 0

Views: 202

Answers (1)

gonso
gonso

Reputation: 2085

The error wasn't sending the user, but the code that checked for it:

user.username

That is not a valid Cloud Code syntaxis. The correct code should look like this:

user.get("username")

Then it all worked fine....

Upvotes: 2

Related Questions