29684

Cloud Code: Creating a Parse.File from URL

Question:

I'm working on a Cloud Code function that uses facebook graph API to retrieve users profile picture. So I have access to the proper picture URL but I'm not being able to acreate a Parse.File from this URL.

This is pretty much what I'm trying:

Parse.Cloud.httpRequest({ url: httpResponse.data["attending"]["data"][key]["picture"]["data"]["url"], success: function(httpImgFile) { var imgFile = new Parse.File("file", httpImgFile); fbPerson.set("profilePicture", imgFile); }, error: function(httpResponse) { console.log("unsuccessful http request"); } });

And its returning the following:

Result: TypeError: Cannot create a Parse.File with that data. at new e (Parse.js:13:25175) at Object.Parse.Cloud.httpRequest.success (main.js:57:26) at Object.<anonymous> (<anonymous>:842:19)

Ideas?

Answer1:

I was having trouble with this exact same problem right now. For some reason this question is already top on Google results for <strong>parsefile from httprequest buffer</strong>!

The <a href="https://www.parse.com/docs/js/api/classes/Parse.File.html" rel="nofollow">Parse.File documentation</a> says

<blockquote>

The data for the file, as 1. an Array of byte value Numbers, or 2. an Object like { base64: "..." } with a base64-encoded String. 3. a File object selected with a file upload control. (3) only works in Firefox 3.6+, Safari 6.0.2+, Chrome 7+, and IE 10+.

</blockquote>

I believe for CloudCode the easiest solution is <strong>2</strong>. The thing that was tripping me earlier is that I didn't notice it expects an Object with the format { base64: {{your base64 encoded data here}} }.

Also Parse.Files can only be set to a Parse.Object after being saved (this behaviour is also present on all client SDKs). I strongly recommend using the Promise version of the API as it makes much easier to compose such asynchronous operations.

So the following code will solve your problem:

<pre class="lang-js prettyprint-override">Parse.Cloud.httpRequest({...}).then(function (httpImgFile) { var data = { base64: httpImgFile.buffer.toString('base64') }; var file = new Parse.File("file", data); return file.save(); }).then(function (file) { fbPerson.set("profilePicture", file); return fbPerson.save(); }).then(function (fbPerson) { // fbPerson is saved with the image });

Recommend

  • Increase heartbeat value in spring rabbit
  • Why won't Cast() work on IEnumerable? [duplicate]
  • How to get real term_meta using ACF with custom fields on taxonomy terms (instead of wp_options)
  • Using MPI_Send/Recv to handle chunk of multi-dim array in Fortran 90
  • Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • How to smoothly connect two signals in matlab [closed]
  • async GET request with body from browser
  • how to add dashed border on highcharts “area” graph for every point
  • How to use array in autohotkey?
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • Generate and export point cloud from Project Tango
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • How to create subsets of a single set of elements with XSLT?
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • How to use Kaminari pagination gem with Sinatra and Mongoid?
  • Runtime complexity of getting the length of a string in different representations
  • Create an average of multiple excel chart without the data source
  • How to split wav file into two or more parts using c#
  • Content-Type alternative in MQTT
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • Print Longitude and Latitude in textbox Android
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • Stacked bar chart with continuous time-axis as x-axis
  • iphone image to video issue in video speed
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • How to encrypt Connectionstring written in web.config from codebehind?
  • How to get rgb from transparent pixel in js
  • Computing the discrete fourier transform of audio data with FFTW
  • Call Microservice from another Microservice within Docker