How can I do pagination when downloading facebook posts via javascript and FB.api?

Kasper Christensen

I am trying to download facebook wall posts via the javascript SDK. I managed to do a call for the first 25 posts, but I get into trouble when I am trying to get paging.next and feed that into a new call using a loop, and then iterate over that until no more pages is available.

The code produces the same page 10 times which I do not understand. It should give the next page and the next page and the next page..?

FB.login(function(response){

        // FIRST CALL THAT CALLS FOR PAGENAME/FEED
        FB.api(request, function (response) {

            // PRINTS TO LOG AND DECLARES X AS THE NEXT PAGE
            console.log(response.paging.next);
            var x = response.paging.next;

            // LOOP THAT PREFERABLY SHOULD CONTINUE UNTIL NO MORE PAGES
            // BUT I WILL DEAL WITH THAT LATER
            for (i = 0; i < 10; i++) {

                // CALLS X WHICH ALSO GIVES ME RHE NEXT PAGE
                // BUT FOR SOME REASON THE CODE DOES NOT MANAGE TO CHANGE
                // X AND DO A NEW CALL
                FB.api(x, function (res){

                    console.log(i);
                    console.log(res.paging.next);
                    // HERE I RESPECIFY X
                    x = res.paging.next;

                    });

                };

           }

          ); 

    }, {scope: 'publish_actions'});
luschn

You need to learn how to deal with asynchronous JavaScript and how to do recursive functions. You are trying to use "x" before the asynchronous API call sets it. Meaning, the whole for loop finishes before "x" even gets set once - because the API call takes a while.

Here´s some quick code, did not test it but it should show one solution:

var items = [];
function apiCall(next) {
    FB.api(next, function (response) {
        for (var i = 0; i < response.data.length; i++) {
            //add all posts to the items array
            items.push(response.data[i]);
        }
        if (response.paging && response.paging.next) {
            //call function recursively until there is no "next"
            apiCall(response.paging.next);
        } else {
            //this is when it´s done
            console.log(items);
        }
    });
}
apiCall('/page-id/feed');

Make sure you understand the concepts, it´s very important to know when you deal with the JavaScript SDK and JavaScript in general.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Unity Facebook API-FB.API()语法

来自分类Dev

通过javascript和FB.api下载Facebook帖子时如何进行分页?

来自分类Dev

通过javascript和FB.api下载Facebook帖子时如何进行分页?

来自分类Dev

使用fb.api的Facebook用户位置

来自分类Dev

使用FB API从Facebook页面获取帖子

来自分类Dev

使用fb.api的Facebook用户位置

来自分类Dev

Using pagination is there a way to get facebook messages via graph api v6.0?

来自分类Dev

How can I deteremine the "category" for a Facebook event?

来自分类Dev

通过不使用FB按钮通过Graph API登录Facebook

来自分类Dev

使用huandu / facebook Golang FB API时出现DecodeField错误

来自分类Dev

如何使用fb sdk 3.14.1登录facebook api ver 1.0?

来自分类Dev

使用FB GRAPH API发布到Facebook组

来自分类Dev

通过不使用FB按钮通过Graph API登录Facebook

来自分类Dev

在Facebook上通过FB.API使用多个分数

来自分类Dev

使用客户端上的FB API发布到Facebook页面

来自分类Dev

Unity facebook SDK, can not post picture with FB.Feed

来自分类Dev

In JIRA Agile, when I click a quickfilter I want other quickfilters to be unclicked, how can I do this?

来自分类Dev

无法通过FB.api签入(javascript)

来自分类Dev

I want to be able to import the JavaMail API and use it in Android Studio. What do I do after downloading mail.jar and activation.jar?

来自分类Dev

failing to do fb like to postId I have just shared successfully

来自分类Dev

How do I replace the behaviour of Web API model binding so that instead of Null I receive a new instance when no parameters are passed in

来自分类Dev

Facebook Graph API位置数据丢失,但在FB网站搜索中可见?

来自分类Dev

通过API增强Facebook帖子并使其反映在FB UI中

来自分类Dev

Facebook图形API / FB UNITY SDK处理应用程序请求(发送和接收)

来自分类Dev

如何使用PHP fb api在两个以上的页面中获取Facebook会话?

来自分类Dev

科尔多瓦Facebook登录FB.api调用不起作用

来自分类Dev

使用 FB api 创建 Facebook 广告时找不到给定广告组的创意规范

来自分类Dev

How Do I set drop down menu list open when I click on parent li Then open the sub li using javascript?

来自分类Dev

将Facebook fb:login-button转换为SDK javascript登录

Related 相关文章

  1. 1

    Unity Facebook API-FB.API()语法

  2. 2

    通过javascript和FB.api下载Facebook帖子时如何进行分页?

  3. 3

    通过javascript和FB.api下载Facebook帖子时如何进行分页?

  4. 4

    使用fb.api的Facebook用户位置

  5. 5

    使用FB API从Facebook页面获取帖子

  6. 6

    使用fb.api的Facebook用户位置

  7. 7

    Using pagination is there a way to get facebook messages via graph api v6.0?

  8. 8

    How can I deteremine the "category" for a Facebook event?

  9. 9

    通过不使用FB按钮通过Graph API登录Facebook

  10. 10

    使用huandu / facebook Golang FB API时出现DecodeField错误

  11. 11

    如何使用fb sdk 3.14.1登录facebook api ver 1.0?

  12. 12

    使用FB GRAPH API发布到Facebook组

  13. 13

    通过不使用FB按钮通过Graph API登录Facebook

  14. 14

    在Facebook上通过FB.API使用多个分数

  15. 15

    使用客户端上的FB API发布到Facebook页面

  16. 16

    Unity facebook SDK, can not post picture with FB.Feed

  17. 17

    In JIRA Agile, when I click a quickfilter I want other quickfilters to be unclicked, how can I do this?

  18. 18

    无法通过FB.api签入(javascript)

  19. 19

    I want to be able to import the JavaMail API and use it in Android Studio. What do I do after downloading mail.jar and activation.jar?

  20. 20

    failing to do fb like to postId I have just shared successfully

  21. 21

    How do I replace the behaviour of Web API model binding so that instead of Null I receive a new instance when no parameters are passed in

  22. 22

    Facebook Graph API位置数据丢失,但在FB网站搜索中可见?

  23. 23

    通过API增强Facebook帖子并使其反映在FB UI中

  24. 24

    Facebook图形API / FB UNITY SDK处理应用程序请求(发送和接收)

  25. 25

    如何使用PHP fb api在两个以上的页面中获取Facebook会话?

  26. 26

    科尔多瓦Facebook登录FB.api调用不起作用

  27. 27

    使用 FB api 创建 Facebook 广告时找不到给定广告组的创意规范

  28. 28

    How Do I set drop down menu list open when I click on parent li Then open the sub li using javascript?

  29. 29

    将Facebook fb:login-button转换为SDK javascript登录

热门标签

归档