Saving an image from a URL in Parse.com using CloudCode

I struggled with saving the file that I get from the HTTP request for several days. 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 am getting 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 strangest part of all this is that the error only occurs when I include the line object.set("icon1", imgFile) I can change the location of the object without any problems. The error only occurs when trying to save imgFile to icon1

Any help would be greatly appreciated. Thanks!

+6
source share
1 answer

According to the documentation ( https://parse.com/docs/js_guide#files ) you need to save the analysis file before you can install it on another object.

Usually:

 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 a sample promise, as it is a little easier when it comes to a series of queries like this.

+4
source

Source: https://habr.com/ru/post/975689/


All Articles