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

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) {

                    }
                });


            });
        }

我有以下代码将多个对象保存到我的parse.com表。 我调试了我的代码和所有内容 该代码使用与最后一个对象相同的信息保存n个对象。 图像正确保存。 p>

图像和标题只是两个长度相同的数组。 p>

我在想这个问题是因为我有 等待前一次保存或php代码的回调完成。 我也在动态更改图像库。 p>

我发现了非常类似的问题,但我尝试了解决方案并且它不起作用: Parse.com保存对象一次 并在此处 只有第一个对象被保存在javascript for循环中 p>

我知道我是什么 在这里做错了吗? p>

  for(i = 0; i&lt; images.length; i ++){
 
 var caption = convert(captions [i]); \  n 
 //将图像更改为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); \  n 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){
 \  n},错误:函数(消息,错误){
 
} 
}); 
 
 
}); 
} 
  code>  pre> 
  div>

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);
});