adolfosrs
adolfosrs

Reputation: 9389

Cant access Facebook Api through Parse Cloud Code

I'm trying to access the facebook api with parse cloud code using javascript. I want to do something very simple, return the events from a given locationId.

So I have this so far:

Parse.Cloud.define("hello", function(request, response) {

    console.log("Logging this");
    Parse.Cloud.httpRequest({
        url: 'https://graph.facebook.com/v2.2/217733628398158/events'   ,
        success: function(httpResponse) {
            console.log("Not logging this");
            console.log(httpResponse.data);
        },
        error:function(httpResponse){
            console.log("Not logging this");
            console.error(httpResponse.data);
        }
    });

  response.success("result");
});

When running this it seems that Parse.Cloud.httpRequest function is failling since is not reaching any log call.

Any idea?

Upvotes: 0

Views: 335

Answers (1)

Patrick Bradshaw
Patrick Bradshaw

Reputation: 536

Dehli's comment is correct. Parse's Cloud Code will not log anything related to alternate threads once response.success has been hit. Since it is located right after the call for the http request, it will actually occur before the request returns, ending the function prematurely.

I would suggest altering your code as such:

Parse.Cloud.define("hello", function(request, response) {
    console.log("Logging this");
    Parse.Cloud.httpRequest({
        url: 'https://graph.facebook.com/v2.2/217733628398158/events',
        success: function(httpResponse) {
            //console.log("Not logging this");
            console.log(httpResponse.data);
            response.success("result");
        },
        error:function(httpResponse){
            //console.log("Not logging this");
            console.error(httpResponse.message);
            response.error("Failed to login");
        }
    });
});

Upvotes: 1

Related Questions