Search code examples
javascriptparse-platformparse-cloud-code

Requesting Function Response in another function


I have those two functions where i call "http" from "Count" the "http" return promise. I want to use the return value of "http" in "Count". What I receive now is Undefined !!! What I'm missing ?

Count Function :

Parse.Cloud.define('count', function(request, response) {

var query = new Parse.Query('MyS');
  query.equalTo("Notify", true);
  query.notEqualTo ("MainEventCode", '5');

  query.find({
    success: function(results) {
      Parse.Cloud.run('http', {params : results}).then(
        function(httpResponse) {
          console.log('httpResponse is : ' + httpResponse.length);
          response.success('Done !');
        }, function(error) {
          console.error(error);
      });
    },
    error: function(error) {
      response.error(error);
    }
  });
});

http Function :

Parse.Cloud.define('http', function(request, response) {

var query = new Parse.Query(Parse.Installation);
.
.
.
}

Solution

  • I think what you're asking is how to use an externally callable cloud function as a step in a bigger cloud procedure. Here's how to do it: (@paolobueno has it essentially correct, with only a couple mistakes in the details).

    First, let's convert that 'http' cloud function to a regular JS function. All we need to do is factor out the request and response objects. (@paolobueno has a very good idea to use underscorejs, but I won't here because its another new thing to learn).

    // for each object passed in objects, make an http request
    // return a promise to complete all of these requests
    function makeRequestsWithObjects(objects) {
        // underscorejs map() function would make this an almost one-liner
        var promises = [];
        for (var i = 0; i < objects.length; i++) {
            var object = objects[i];
            promises.push(makeRequestWithObject(object));
        }
        return Parse.Promise.when(promises);
    };
    
    // return a promise to do just one http request
    function makeRequestWithObject(object) {
        var url = 'http://185.xxxxxxx'+ object +'&languagePath=en';
        return Parse.Cloud.httpRequest({ url:url });
    }
    

    It looks like you want the updated cloud function -- rather than use params from the client -- to first make a query and use the results of that query as parameters to the http calling function. Here's how to do that. (Again, using @paolobueno's EXCELLENT practice of factoring into promise-returning functions...)

    // return a promise to find MyS instances
    function findMyS() {
        var query = new Parse.Query('MyS');
        query.equalTo("Notify", true);
        query.notEqualTo ("MainEventCode", '5');
        return query.find();
    }
    

    Now we have everything needed to make a clear, simple public function...

    Parse.Cloud.define('count', function(request, response) {
        findMyS().then(function(objects) {
             return makeRequestsWithObjects(objects);
        }).then(function(result) {
            response.success(result);
        } , function(error) {
            response.error(error);
        });
    });