1

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...

Grant Park
  • 1,004
  • 1
  • 9
  • 29
  • If it helps any, I tried console.log on theimage and it comes out as {"__type":"File"} – Grant Park Dec 30 '15 at 06:47
  • I've also looked at Parse's js docs and found how to work with Files, but I don't know how to convert a PFFile to a Parse.File https://parse.com/docs/js/guide#files – Grant Park Dec 30 '15 at 06:49

1 Answers1

0

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);
});
Grant Park
  • 1,004
  • 1
  • 9
  • 29
  • how secure is it to pass a user name and password like this through cloud code? – Peter Kaminski Jun 19 '16 at 18:55
  • as safe as an http request: http://stackoverflow.com/questions/1008668/how-secure-is-a-http-post, that said, Parse is dead and I wouldn't spend too long racking your brains over security at this point – Grant Park Jun 19 '16 at 20:28
  • Trying to migrate my current application over to a self hosted server, so for now it's got to be secure – Peter Kaminski Jun 19 '16 at 21:16
  • @GrantPark How did you cast that? Do you have a sample from PFFile to Nsdata. I cant get it right. – Cliffordwh Nov 23 '16 at 15:47