Parse.com文件未更新,导致数据浏览器链接断开

时间:2014-07-18 22:28:05

标签: javascript parse-platform

所以在我的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.
            });
        }
    });

1 个答案:

答案 0 :(得分:1)

在尝试将文件分配给解析对象之前,您似乎没有等待文件保存。 Async JS每次都会帮到你。

您应该只在保存之后将文件对象保存在解析对象中。您可以将两个文件保存放在一个数组中,也可以使用Parse.Promise.when([promise1, promise2]).then(...);