6

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

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

4

1 回答 1

4

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

于 2014-09-23T06:54:29.463 回答