Search code examples
iosswiftparse-platformparse-cloud-code

Setting PFFile on Object in Parse Cloud Code


Is it possible to directly pass PFFiles in Swift to Cloud Code and set it on an object?

I've got a PFFile,

let imageData = UIImageJPEGRepresentation(self.imageView.image!, 0.1)
let imageFile = PFFile(data: imageData!)

and I'm trying to set it on a user object before signing it up in cloud code like so:

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

    var theimage = request.params.image
    var username = request.params.username
    var pw = request.params.password

    var user = new Parse.User();
    user.set("username", username);
    user.set("password", pw);

    theimage.save().then(function(){
        user.set("image",theimage)
        user.signUp(null, {
        success: function(user) {       
            response.error("working");
        },
        error: function(user, error) {
            response.error("Sorry! " + error.message);
        } });

    }, function(error) {
        response.error("Sorry! " + error.message);
    });

});

iOS code to call cloud code:

 PFCloud.callFunctionInBackground("createNewUser", withParameters: 
 ["username": username, "password": password, "image": imageFile], 
 block: { (result, error) -> Void in
    // UI stuff
 })

I'm getting this error:

TypeError: Cannot read property 'format' of undefined
at e.a.value (Parse.js:13:25673)
at main.js:40:14 (Code: 141, Version: 1.10.0)

I tried looking at this: http://parse.com/docs/js/api/classes/Parse.File.html and I'm still not sure how I'm going to get my image from Swift to cloud code so I can sign up the new user...


Solution

  • Instead of passing the image as a PFFile, I passed in the image as NSData and subsequently converted the result to a Parse.File object. Then I saved it and signed the user up. Works well!

    JS

    var parseFile = new Parse.File("prof",theimage)
    
    parseFile.save().then(function(){
        user.set("image",parseFile)
        user.signUp(null, {
        success: function(user) {       
            response.success("working");
        },
        error: function(user, error) {
            response.error("Sorry! " + error.message);
        } });
    
    }, function(error) {
        response.error("Sorry! " + error.message);
    });