node-facebook / facebook-node-sdk

Modeled from the (Facebook Javascript SDK), now with the facebook-node-sdk you can now easily write the same code and share between your server (nodejs) and the client (Facebook Javascript SDK).
Other
529 stars 114 forks source link

after param doesn't seem to work #169

Closed andricicezar closed 6 years ago

andricicezar commented 6 years ago

I'm trying to take all the photos from an album, but I can't pass the first page. The after cursor is set correctly, but FB.api doesn't seem to take the after parameter in consideration.

My code creates an infinite loop: (node 10.0.0 required)

  const photos = async function* (after) {
    const result = await new Promise((resolve, reject) =>
      fb.api(
        `${rawAlbum.id}`,
        'GET',
        {
          fields: 'photos{id}',
          after: after,
        },
        (result) => {
          if (result.error) reject(result.error);

          resolve(result);
        }
      )
    );

    for (let i = 0, len = result.photos.data.length; i < len; ++i) {
      yield result.photos.data[i];
    }

    if (result.photos.paging.next) {
      yield *photos(result.photos.paging.cursors.after);
    }
  };

Code example from Graph API Explorer:

FB.api(
  '/1659613387490634/photos',
  'GET',
  {"fields":"id","limit":"5","after":"MTY1OTY1MjMxNzQ4Njc0MQZDZD"},
  function(response) {
      // Insert your code here
  }
);

I don't see any difference between the example and my code, and for some reasons, it doesn't work with this package. Thank you!

dantman commented 6 years ago

This SDK doesn't do anything special with params, it just serializes all of them and passes them on. So it shouldn't be missing one param but not another.

I suggest you try debugging by breaking the loop after a few iterations (to avoid debugging while an infinite loop goes on), add a console.log to see what the actual value of result.photos.paging you are getting is, and use DEBUG=fb:* to see what requests the library is making.

andricicezar commented 6 years ago

Thank you for fast response. You're right, this package worked correctly, but I used wrong the Graph. It seems that the problem was that I was using /${albumId} with the fields photos{id} and I was supposed to use /${albumId}/photos with the fields id. Thank you very much for your help!