0

我试图在 facebook javascript sdk 中显示签入的标签,但只显示 [object Object] 因为它是一个数组,我尝试了几种方法来显示这个数组,但不能。

以下代码:

<!DOCTYPE html> 
<html xmlns:fb="https://www.facebook.com/2008/fbml">
    <head>
        <title>New JavaScript SDK</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                window.fbAsyncInit = function() {
                    FB.init({
                        appId: '274371602680881', 
                        status: true, 
                        cookie: true,
                        xfbml: true,
                        oauth: true
                    });

                    function updateButton(response) {
                        var button = document.getElementById('fb-auth');

                        if (response.authResponse) {
                            //user is already logged in and connected
                            var userInfo = document.getElementById('user-info');
                            FB.api('/me', function(response) {
                                userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
                                button.innerHTML = 'Uninstall';

                                FB.api('/me/checkins', function(response) {
                                    console.log('Got your check-ins: ', response);

                                    if (!response.error) {
                                        //displayCheckIns(response.data, document.getElementById('checkins'));

                                        var markup = '<div class="data-header">Your last five check-ins:</div>';

                                        for (var i=0; i < response.data.length && i < 5; i++) {
                                            var checkin = response.data[i];

                                            //alert(checkin.tags);
                                            //alert(checkin.tags.length);
                                            //alert(checkin.tags.name);
                                            //alert(checkin.tags[0]);
                                            //alert(checkin.tags[0].name);

                                            markup += '<div class="place">'
                                                        + '<div class="picture">Foto: <img src="http://graph.facebook.com/' + checkin.place.id + '/picture"></div>'
                                                        + '<div class="info">'
                                                            + '<div class="from">From: ' + checkin.from.name + '</div>'
                                                            + '<div class="tags">Tags: ' + checkin.tags + '</div>'
                                                            + '<div class="place">Place: ' + checkin.place.name + '</div>'
                                                            + '<div class="place-location">Place Location: ' + checkin.place.location.latitude + ", " + checkin.place.location.longitude + '</div>'
                                                            + '<div class="application">Application: ' + checkin.application.name + '</div>'
                                                            + '<div class="created_time">Created time: ' + checkin.created_time + '</div>'
                                                            + '<div class="likes">Likes: ' + checkin.likes + '</div>'
                                                            + '<div class="check-in-msg">Mensagem: ' + (checkin.message || '') + '</div>'
                                                            + '<div class="comments">Comments: ' + checkin.comments + '</div>'
                                                        + '</div>'
                                                    + '</div>';
                                        }

                                        document.getElementById('user-checkins').innerHTML = markup;
                                    }
                                });
                            });
                            button.onclick = function() {
                                FB.api({
                                    method: 'auth.revokeAuthorization'
                                }, function(response) {
                                    window.location.reload();
                                });
                                /*
                                FB.logout(function(response) {
                                    var userInfo = document.getElementById('user-info');
                                    userInfo.innerHTML="";
                                });*/
                            };
                        } else {
                            //user is not connected to your app or logged out
                            button.innerHTML = 'Login';
                            button.onclick = function() {
                                FB.login(function(response) {
                                    if (response.authResponse) {
                                        FB.api('/me', function(response) {
                                            var userInfo = document.getElementById('user-info');
                                            userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture" style="margin-right:5px"/>' + response.name;
                                        });
                                    } else {
                                        //user cancelled login or did not grant authorization
                                    }
                                }, {
                                    scope:'user_status'
                                });
                            }
                        }
                    }

                    // run once with current status and whenever the status changes
                    FB.getLoginStatus(updateButton);
                    FB.Event.subscribe('auth.statusChange', updateButton);
                };

                (function() {
                    var e = document.createElement('script'); e.async = true;
                    e.src = document.location.protocol 
                        + '//connect.facebook.net/en_US/all.js';
                    document.getElementById('fb-root').appendChild(e);
                }());
            });
        </script>
    </head>
<body> 

<div id="fb-root"></div>
<h2>Updated JS SDK example</h2><br />
<div id="user-info"></div>
<div id="user-checkins"></div>
<p><button id="fb-auth">Login</button></p>
</body> 
</html>

我尝试了几种方法,例如:

checkin.tags // shows [object Object]
checkin.tags.length // shows undefined
checkin.tags.name // shows undefined
checkin.tags[0] // shows undefined
checkin.tags[0].name // Can not read property 'name' of undefined

这发生在标签、喜欢和评论上。其他不是数组的工作正常,例如“checkin.place.name”,甚至“checkin.place.location.latitude”都会带来正确的值。

我做了一个“console.log(JSON.stringify(checkin.tags));” 它返回:

{"data":[{"id":"100001702765878","name":"Mauricio Forte Neto"},{"id":"100001174670611","name":"Mario Celso"}],"paging":{"next":"https://graph.facebook.com/402127889842448/tags?access_token=AAAD5ih3qDDEBAIo3nB8P3ZCAwXrivw5lunDRAUvkRZCCFZBUy5au3ImicViq80HHVZC29hLDit6QwlZCXZB5WEBkDF3Pp2DrnUGWLrAwhpWddLoliyFDqd&limit=25&offset=25&__after_id=100001174670611"}}

请帮忙。

提前致谢。

4

1 回答 1

0

由于它是一个对象并且其中包含数据,因此我必须请求其中的数据类型。

我缺少的是添加一个“.data”,例如:

checkin.tags.data[0].name

现在可以了!!!

于 2012-10-04T18:36:04.390 回答