所以在我的js代码中,用户正在制作马赛克图像。当他们按下上传时,我创建了一个新对象,一切都很好,我的代码运行完美。然后我将 id 保存到刚刚上传的对象中。如果用户再次按下上传按钮,我只想使用相同的对象 ID 并清除之前保存的文件并使用最新版本。我所有的保存、更新以及一切似乎都运行良好。当我进入数据浏览器时,我看到了文件,但当我单击时,链接被破坏。 (这似乎根本不可能)。我的代码 95% 都在下面,除此之外还有一些东西可以生成马赛克图像并存储返回的 id。如何避免这些损坏的链接?
var ins_file = new Parse.File("Instructions.png", { base64: img_ins_InBase64 }, "image/png");
var pretty_file = new Parse.File("Pretty.png", { base64: img_pretty_InBase64 }, "image/png");
ins_file.save().then(function () {
}, function (error) {
console.log("Instruction File couldn't be saved")
// The file either could not be read, or could not be saved to Parse.
});
pretty_file.save().then(function () {
}, function (error) {
console.log("Mosaic File couldn't be saved")
// The file either could not be read, or could not be saved to Parse.
});
var mosaicClass = Parse.Object.extend("Mosaics");
var mosaicObj = new Parse.Object("Mosaics");
var query = new Parse.Query(mosaicClass);
query.get(parseId, {
success: function (objToUpdate) {
// The object was retrieved successfully. SIMPLY UPDATE
objToUpdate.set("img_ins", ins_file);
objToUpdate.set("img_pretty", pretty_file);
objToUpdate.save().then(function () {
console.log("Initial Image updated");
var ins_img_url = objToUpdate.get('img_ins').url();
var pretty_img_url = objToUpdate.get('img_pretty').url();
objToUpdate.set("img_ins_url", ins_img_url);
objToUpdate.set("img_pretty_url", pretty_img_url);
objToUpdate.save();
console.log("Mosaic updated, id was: " + objToUpdate.id);
parseId = objToUpdate.id;
}, function (error) {
console.log("File couldn't be updated")
// The file either could not be read, or could not be saved to Parse.
});
},
error: function (object, error) {
// The object was not retrieved successfully.
// parseId was null so make a new thing
mosaicObj.set("img_ins", ins_file);
mosaicObj.set("img_pretty", pretty_file);
mosaicObj.save().then(function () {
console.log("Initial Images uploaded");
var ins_img_url = mosaicObj.get('img_ins').url();
var pretty_img_url = mosaicObj.get('img_pretty').url();
mosaicObj.set("img_ins_url", ins_img_url);
mosaicObj.set("img_pretty_url", pretty_img_url);
mosaicObj.save();
console.log("Mosaic Saved, id was: " + mosaicObj.id);
parseId = mosaicObj.id;
}, function (error) {
console.log("File couldn't be saved")
// The file either could not be read, or could not be saved to Parse.
});
}
});
最佳答案
看来您在尝试将文件分配给解析对象之前没有等待文件保存。 Async JS 每次都会帮助您。
您应该仅在保存之后在 then 内部将文件对象保存在解析对象上。您可以将两个文件保存在一个数组中并使用 Parse.Promise.when([promise1, Promise2]).then(...);
。
关于javascript - Parse.com 文件未更新,导致数据浏览器的链接损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834707/