0

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?

adolfosrs
  • 9,286
  • 5
  • 39
  • 67

1 Answers1

1

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");
        }
    });
});
Patrick Bradshaw
  • 536
  • 4
  • 10
  • Ok. Just 2 more concerns: 1. In this url i'm using i'm not giving my application access token. And so my Request is returning error code 104: "An access token is required to request this resource.". So, how I'm supposed to give this information? 2. And also, i'm looking to call this httpRequest n times. And since I have the response.success i cant see any way to do this. How could I make it work? Thanks. – adolfosrs Jan 01 '15 at 14:57
  • I would suggest taking a look at this question: . Abstracting from the url there, you could use "https://graph.facebook.com/v2.2/217733628398158/events?fields={enter fields you want here}&access_token={access token}" As for calling this n times, are you trying to do that all within one call? I'm unsure whether you want to call this n times with n different tokens (which you would call "hello" multiple times on your client) or n times with the same token (where calling on parse makes more sense). – Patrick Bradshaw Jan 02 '15 at 03:21
  • Thanks for your reply Patrick. I will open a new question for the second question so it will be more clear.. – adolfosrs Jan 02 '15 at 18:46