Ank*_*wal 6 javascript httprequest parse-platform pffile parse-cloud-code
我一直在努力保存我从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);
}
});
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
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)
Run Code Online (Sandbox Code Playgroud)
关于这一切的最奇怪的部分是,只有当我包含object.set("icon1", imgFile)
我能够修改object无问题位置的行时才会出现错误.仅当我尝试保存imgFileto 时才会发生错误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");
});
Run Code Online (Sandbox Code Playgroud)
我还重写了这部分函数来说明 Promise 模式,因为在处理这样的一系列请求时会更容易一些。
| 归档时间: |
|
| 查看次数: |
2829 次 |
| 最近记录: |