Search code examples
parse-platformparse-cloud-code

Passing in Parse Object into a cloud code function


I have a cloud code function that creates a GuestlistInvite

Object. It takes a phoneNumber, a guestlist Object, and a guest Object.

I call on the function like so:

Parse.Cloud.run('createGuestlistInviteForExistingUser', {
   phoneNumber: phoneNumber,
   guestlist: guestlist,
   guest: user
   }).then(function(guestlistInvite) {
      response.success(successfully created guestlistInvite');
 }); 

Both the guestlist and user are pointers. However in my logs I receive an error:

Result: Error: Parse Objects not allowed here

Any idea why this is happening?

Parse.Cloud.define('createGuestlistInviteForExistingUser', function(request, response) {

  var phoneNumber = request.params.phoneNumber;
  var guestlist = request.params.guestlist;
  var guest = request.params.guest;

  var guestlistInvite = new Parse.Object("GuestlistInvite");

  guestlistInvite.save({
    phoneNumber: phoneNumber,
    Guestlist: guestlist,
    Guest: guest,
    checkInStatus: false,
    response: 0
  }).then(function(guestlistInvite) {
    console.log('guestlistInvite for existing user was created');
    response.success(guestlistInvite);
  }, function(error) {
    response.error('guestlistInvite was not saved');
  });

});

Solution

  • You can't send whole PFObject as a request parameter to call Cloud Functions.

    But You can achieve that functionality simply by passing PFObject's objectId as a request parameter and then write code in cloud code to get object from objectId in Cloud code.

    Here's a snipped of what the cloud code may look like, utilizing promises:

    Parse.Cloud.define("myFunction", functoin(request, response)
    {
        var MyObject = Parse.Object.extend("MyObjectClass"); //You can put this at the top of your cloud code file so you don't have to do it in every function
        var myObject = new MyObject(); 
        var myObjectId = request.params.myObjectId;
    
        myObject.id = myObjectId;
        myObject.fetch().then
        (
            function( myObject )
            {
                //do stuff with it
            },
            function( error )
            {
                response.error("There was an error trying to fetch MyObjectClass with objectId " + myObjectId + ": " + error.message);
            }
        );
    });