简体   繁体   中英

Parse Promises Multiple httpRequest Cloud Code

I'm writing an iOs app with Parse.com and Cloud Code. 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 . 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 !

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. Otherwise , your promise array would be assigned to be a new blank array each loop.

  2. Parse.File would be saved automaticly when its parent do save, you don't need to save it in advance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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