6

I have been struggling with saving a file I retrieve from an HTTP Request for a a few days now. Here is my code:

    Parse.Cloud.httpRequest({
    url: "https://ss1.4sqi.net/img/categories_v2/food/vietnamese_88.png",
    success: function(httpImgFile) 
    {
        console.log("httpImgFile: " + String(httpImgFile.buffer));
        var imgFile = new Parse.File("imgFile1.png", httpImgFile.buffer);                                               
        object.set("location", "newYork"); //object is a PFObject retrieved earlier
        object.set("icon1", imgFile);
        object.save(null, {
          success: function(gameScore) {
            response.success("saved object");
          },
          error: function(gameScore, error) {
            response.error("failed to save object");
          }
        });     
    },
    error: function(httpResponse) 
    {
        console.log("unsuccessful http request");
        response.error(responseString);
    }
});

The error I get is:

Failed with: TypeError: Cannot call method 'then' of undefined
    at Object.b.File.save (Parse.js:2:14963)
    at null.<anonymous> (Parse.js:2:30041)
    at e (Parse.js:2:6339)
    at Parse.js:2:7092
    at g (Parse.js:2:6829)
    at c.extend.then (Parse.js:2:7077)
    at Parse.js:2:30016
    at Array.forEach (native)
    at Function.x.each.x.forEach (Parse.js:1:661)
    at Function.b.Object._deepSaveAsync (Parse.js:2:29993)

The weirdest part about all this is that the error only occurs when I include the line object.set("icon1", imgFile) I am able to to modify the location of object without problem. The error occurs solely when I try to save the imgFile to icon1

Any help would be greatly appreciated. Thanks!

2
  • where is your object variable coming from? What is the type of object? Commented Sep 23, 2014 at 4:39
  • object is a PFObject of type "OuterList". That probably won't help you much though Commented Sep 23, 2014 at 16:15

1 Answer 1

4

As per the documentation (https://parse.com/docs/js_guide#files) you have to save the Parse File before you can set it on another object.

Typically:

imgFile.save().then(function () {

    ...
    object.set("icon1", imgFile);

    return object.save();

}).then(function (gameScore) {
    response.success("saved object");
}, function (error) {
   response.error("failed to save object");
});

I also rewrote this part of the function to illustrate the promise pattern, as it is a little easier when dealing with a series of requests like this.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Tom. Thanks for the suggestion. I tried saving the imgFile before setting like you described but I'm still encountering the same error unfortunately. Do you think it has something to do with the format of the downloaded imgFile?
Can you try the following var imgFile = httpImgFile.buffer.toString('base64');
Thanks for the suggestion Tom. Unfortunately, still no luck: Error: Uncaught Creating a Parse.File from a String is not yet supported. (Code: 141, Version: 1.2.21) entered result block
Try var imgFile = new Parse.File("imgFile1.png", {base64: httpImgFile.buffer.toString('base64')});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.