云代码:从URL创建一个Parse.File [英] Cloud Code: Creating a Parse.File from URL

查看:89
本文介绍了云代码:从URL创建一个Parse.File的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用Facebook图形API检索用户个人资料图片的Cloud Code函数.因此,我可以访问正确的图片URL,但是无法从该URL创建Parse.File.

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");
        }
    });

及其返回以下内容:

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)

想法?

推荐答案

我现在遇到了与此完全相同的问题.出于某种原因,这个问题已经是Google搜索结果中来自httprequest缓冲区的解析文件了!

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

Parse.File文档

文件的数据,例如1.字节值Numbers的数组,或2.一个具有base64编码的String的对象,例如{base64:"..."}. 3.使用文件上载控件选择的File对象. (3)仅适用于Firefox 3.6 +,Safari 6.0.2 +,Chrome 7+和IE 10 +.

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

我相信对于CloudCode,最简单的解决方案是 2 .之前令我绊倒的是,我没有注意到它期望格式为{ base64: {{your base64 encoded data here}} }Object.

I believe for CloudCode the easiest solution is 2. 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}} }.

Parse.File也只能在保存后设置为Parse.Object(所有客户端SDK上也存在此行为).我强烈建议您使用API​​的Promise版本,因为它可以更轻松地编写此类异步操作.

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:

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
});

这篇关于云代码:从URL创建一个Parse.File的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆