我一直在努力保存我从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
非常感谢任何帮助。谢谢!
答案 0 :(得分: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模式,因为在处理这样的一系列请求时它更容易。