0

I'm trying to do an api call to facebook (using angularJs. Here's my code (taken from the service):

fbTest: function (name) {
    resource = $resource('http://graph.facebook.com/:id?fields=id,name,picture', { id: '@id' });
    return resource.get({ id: name });
}

It returns the data just fine: (inspected with Fiddler)

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: application/json; charset=UTF-8
ETag: "c49e5dcad5a864ff2e16bb3af547231f725c05e7"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 1002313
X-FB-Debug: tMeW4nnfiRBj6VK+QRtbe0YdaGfQfkXcaWL4VzXTXxw=
Date: Mon, 11 Nov 2013 16:53:29 GMT
Connection: keep-alive
Content-Length: 241

{
   "id": "Data",
   "name": "Data",
   "picture": {
      "data": {
         "url": "Data",
         "is_silhouette": false
      }
   }
}

However how can I access the properties? I tried writing .name but that doesn't return anything.

Jordan Axe
  • 3,823
  • 6
  • 20
  • 27
  • See this article: [http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails][1] [1]: http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails – NM Pennypacker Nov 11 '13 at 16:59
  • @NickM I looked at it but I don't get what I'm supposed to do. – Jordan Axe Nov 11 '13 at 17:01

2 Answers2

1

The documentation is a little unclear to me, but it seems like some of the exposed methods such as .get execute asynchronously. What this means is that the return value from .get may not actually be returning the data from HTTP.

Instead, you need to do something like:

resource.get({ id: name }, function (data) {
    // do something with data.name, etc.
});
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

It happens because the result of the get isn't ready when you return the function. You are returning a promise.

Try binding the $resource.get to some attribute in your $scope and, when ready, it will be displayed correctly.

Beterraba
  • 6,515
  • 1
  • 26
  • 33