Search code examples
parse-platformhttprequestparse-cloud-code

How do I create and call the httpRequest function in a parse.com job?


I have created a Job in cloud code, as well as defined a cloud code function that is an http request. As below, I have the http request defined in the job, and I can run this job successfully, but I can't get the httpRequest function to print to the logs, in order to see that the code is actually being run. Hence, I can't really tell that the job is using the http request. How can I get this http request to print to the parse.com logs so that I can see that it is actually working?

//lets define the job
Parse.Cloud.job("simpleGetRequest", function(request, response) {

//lets try to define the httpRequest function.
Parse.Cloud.define("httpRequest", function(request, response) {
    return Parse.Cloud.httpRequest({
            url: "https://data.seattle.gov/resource/y7pv-r3kh.json?$select=date_reported",
            success: function(httpResponse) {
                response.success(httpResponse.text);
                //console.log(httpResponse);
                //lets just see if we can get the console to do anyting
                console.log("we made it to this line")
            },
            error: function(httpResponse) {
                response.error('request failed with response code ' + httpResponse)
            }
    });
});
    //we need to call this response.success to make things happen. lets put this at the end always so we know if at least hte job is a success
    response.success("The scheduled job completed.");
});

Solution

  • There's more you could do to improve the function, but to answer the specific problem, reorder the console logs to execute before calling response success, which terminates the job immediately...

    console.log(httpResponse);
    //lets just see if we can get the console to do anyting
    console.log("we made it to this line")
    response.success(httpResponse.text);
    

    Furthermore, do not invoke response success on that last line of the function. This too, terminates the job before the http request completion block has a chance to execute.

    Finally, not sure how you're invoking the job or the function, but its structure is highly unusual, nesting the definition of a cloud function in a job. You should choose whether you want a function to run when invoked by your app (cloud function) or a function that is run by the system on a schedule (job). Here's what each would look like (using promises, which is a necessary habit once you start doing more than a couple things asynchronously in the cloud)...

    Call this job by "scheduling" on the parse web app...

    Parse.Cloud.job("simpleGetRequest", function(request, response) {
        Parse.Cloud.httpRequest().then(function(httpResponse) {
            console.log(httpResponse);
            //lets just see if we can get the console to do anyting
            console.log("we made it to this line")
            response.success(httpResponse.text);
        }, function(httpResponse) {
            response.error('request failed with response code ' + httpResponse)
        });
    });
    

    Call this function from app logic, depending on which sdk, with some variety of Parse.Cloud.run()...

    Parse.Cloud.define("simpleGetRequest", function(request, response) {
        Parse.Cloud.httpRequest().then(function(httpResponse) {
            console.log(httpResponse);
            //lets just see if we can get the console to do anyting
            console.log("we made it to this line")
            response.success(httpResponse.text);
        }, function(httpResponse) {
            response.error('request failed with response code ' + httpResponse)
        });
    });