dongxing7530 2015-03-21 09:38
浏览 48
已采纳

Parse.com只在一个循环中保存对象,只保存最后一个对象

I have the following code to save multiple objects to my parse.com table. I have debugged my code and everything should be working properly. This code is saving n objects all with the same information as the last object. The images are being save correctly.

Images and captions are just two arrays with the same length.

I am thinking the problem is because I have to wait for the callback from the previous save or the php code to finish. I am changing the image base on the fly as well.

I saw very similar problems posted but I tried the solution and it did not work: Parse.com save object once and here only first object getting saved in javascript for loop

Any idea what I am doing wrong here?

for(i = 0; i < images.length; i++){

            var caption = convert(captions[i]);

            //change image to base64
            $.post("base64.php", {base64: images[i]}, function(data){

                var image = data;

                var post = new Post();

                post.set("post", id);
                post.set("number", i + 1);
                post.set("by", by);
                post.set("title", title);
                post.set("description", description);
                post.set("caption", caption);
                post.set("views", 0);
                post.set("type", "image");

                var parseImage = new Parse.File("image.png", {base64: image});
                post.set("image", parseImage);

                post.save(null, {
                    success: function(messages) {

                    },error: function(messages, error) {

                    }
                });


            });
        }
  • 写回答

1条回答 默认 最新

  • dongxuandong2045 2015-03-22 19:11
    关注

    Batch them up:

    var posts = [];
    for(i = 0; i < images.length; i++){
        var caption = convert(captions[i]);
    
        //change image to base64
        $.post("base64.php", {base64: images[i]}, function(data){
    
            var image = data;
    
            var post = new Post();
    
            post.set("post", id);
            post.set("number", i + 1);
            post.set("by", by);
            post.set("title", title);
            post.set("description", description);
            post.set("caption", caption);
            post.set("views", 0);
            post.set("type", "image");
    
            var parseImage = new Parse.File("image.png", {base64: image});
            post.set("image", parseImage);
    
            // push to array for batch saving
            posts.push(post);
        });
    }
    Parse.Object.saveAll(posts)
    .then(function() {
        console.log('all saved');
    }, function(error) {
        console.error(error);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)