简体   繁体   English

解析承诺多个httpRequest云代码

[英]Parse Promises Multiple httpRequest Cloud Code

I'm writing an iOs app with Parse.com and Cloud Code. 我正在使用Parse.com和Cloud Code编写iOs应用程序。 Actually I want to retrieve objects which contain one picture and other informations from a website and I want to add them to a class named News . 实际上,我想从网站检索包含一张图片和其他信息的对象,并将它们添加到名为News的类中。 When I run my code, every object is saved (in my class, one row = one retrieved object) but unfortunately the only first one has its picture saved.... Any idea ? 当我运行代码时,每个对象都被保存(在我的类中,一行=一个检索到的对象),但不幸的是,唯一的第一个对象已保存了其图片。

I made a lot of searches about promises (series / parallels) and I think the problem comes from here.. 我做了很多关于诺言(串联/并联)的搜索,我认为问题出在这里。

Note : don't worry about myLink, myImgLink : I put this to make my code easy to read ! 注意:不用担心myLink,myImgLink :我这样做是为了使我的代码易于阅读!

Parse.Cloud.define("rajouteNews", function(request, response) {
    Parse.Cloud.httpRequest({ url: 'myUrl'}).then(function(httpResponse) {

            var news = [];
            var NewsClass = Parse.Object.extend("news");
            for (var i = 0; i < 10 ; ++i) {
                var maNews = new NewsClass();
                maNews.set("link", myLink[i]); // "Other informations"
                maNews.set("imgLink", myImgLink[i]);
                maNews.set("title", myTitle[i]);

                var promises = [];
                promises.push(Parse.Cloud.httpRequest({
                    url: $('img').attr('src'),
                    method: 'GET',
                }).then(function(httpResponse){
                        var imgFile = new Parse.File("photo.jpg", {base64:httpResponse.buffer.toString('base64')});
                        maNews.set("image",imgFile); // The picture
                        return maNews.save();
                }));
                news.push(maNews);  
            }               
            promises.push(Parse.Object.saveAll(news, {
                success: function (list) {
                    response.success(news.length.toString() + " ont été sauvegardées");

                },
                error: function (list, err) {
                    response.error("Error adding news");
                }
            }));
            return Parse.Promise.when(promises);

        }).then(function(bla,result){
            response.success("Job done");       
        }, function(error) {
        response.error(error);
    }
);
});
  1. Your promises array should put out of the for loop scope. 您的promises数组应放在for循环范围之外。 Otherwise , your promise array would be assigned to be a new blank array each loop. 否则,您的promise数组将被分配为每个循环新的空白数组。

  2. Parse.File would be saved automaticly when its parent do save, you don't need to save it in advance. Parse.File的父项确实保存时, Parse.File会自动保存,您无需事先保存。

So I improve your code as following, try it and tell me weather it works. 因此,我将按照以下方式改进您的代码,尝试一下并告诉我它可以正常工作。

Parse.Cloud.define("rajouteNews", function(request, response) {
    Parse.Cloud.httpRequest({
      url: 'myUrl'
    }).then(function(httpResponse) {
      var promises = [];
      var NewsClass = Parse.Object.extend("news");
      for (var i = 0; i < 10; ++i) {
        var maNews = new NewsClass();
        maNews.set("link", myLink[i]); // "Other informations"
        maNews.set("imgLink", myImgLink[i]);
        maNews.set("title", myTitle[i]);

        var maNewsPromise = Parse.Cloud.httpRequest({
          url: $('img').attr('src'),
          method: 'GET',
        }).then(function(httpResponse) {
          var imgFile = new Parse.File("photo.jpg", {
            base64: httpResponse.buffer.toString('base64')
          });
          maNews.set("image", imgFile); // The picture
          return maNews.save();
        });
        promises.push(maNewsPromise);
      }
      return Parse.Promise.when(promises)
    }).then(function(bla, result) {
      // this function is call when `Parse.Promise.when(promises)` is done,
      //I can't figure out why you take two params.
      response.success("Job done");
    }, function(error) {
      response.error(error);
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM