使用CloudCode从Parse.com中的URL保存图像



几天来,我一直在努力保存从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 时发生

如有任何帮助,我们将不胜感激。谢谢

根据文档(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模式,因为在处理这样的一系列请求时会更容易一些。

最新更新