【问题标题】:Saving an Image from URL in Parse.com using CloudCode使用 CloudCode 从 Parse.com 中的 URL 保存图像
【发布时间】:2014-11-17 02:23:44
【问题描述】:

几天来,我一直在努力保存从 HTTP 请求中检索到的文件。这是我的代码:

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

我得到的错误是:

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)

关于这一切的最奇怪的部分是,只有当我包含 object.set("icon1", imgFile) 行时才会出现错误 我可以毫无问题地修改object 的位置。该错误仅在我尝试将imgFile 保存到icon1 时发生

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您的object 变量来自哪里? object的类型是什么?
  • object 是“OuterList”类型的 PFObject。不过,这可能对您没有多大帮助

标签: javascript parse-platform httprequest parse-cloud-code pffile


【解决方案1】:

根据文档 (https://parse.com/docs/js_guide#files),您必须先保存解析文件,然后才能将其设置在另一个对象上。

通常:

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

我还重写了这部分函数来说明 Promise 模式,因为它在处理这样的一系列请求时更容易一些。

【讨论】:

  • 嗨,汤姆。谢谢你的建议。我尝试在按照您描述的设置之前保存 imgFile,但不幸的是我仍然遇到相同的错误。你觉得和下载的imgFile的格式有关系吗?
  • 你可以试试下面的 var imgFile = httpImgFile.buffer.toString('base64');
  • 感谢汤姆的建议。不幸的是,仍然没有运气:错误:尚不支持从字符串创建 Parse.File。 (代码:141,版本:1.2.21)进入结果块
  • 试试 var imgFile = new Parse.File("imgFile1.png", {base64: httpImgFile.buffer.toString('base64')});
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2016-01-12
  • 2012-03-30
相关资源
最近更新 更多