morantis
morantis

Reputation: 106

Javascript Facebook Variables Issue

Ok, so I know Javascript and have been piecing together a small app for Facebook. The problem is somewhere between my JS and the facebook api.

I used the standard library to populate an array with a users friend list and display them for choosing.

function renderMFS() {
// First get the list of friends for this user with the Graph API
FB.api('/me/friends', function(response) {
    var container = document.getElementById('mfs');
    var mfsForm = document.createElement('form');
    mfsForm.id = 'mfsForm';

    // Iterate through the array of friends object and create a checkbox for each one.
    for (var i = 0; i < Math.min(response.data.length, 1000); i++) {
        var friendItem = document.createElement('div');
        friendItem.id = 'friend_' + response.data[i].id;
        friendItem.innerHTML = '<input type="radio" onclick="sendSlap(this)" name="friends" value="' + response.data[i].id + '" />' + response.data[i].name;

        mfsForm.appendChild(friendItem);

    }

    container.appendChild(mfsForm);
    });

}

And then I used a function of my own to post two feed messages.

function sendSlap(cb) {



FB.ui({
    method : 'feed',
    link: 'https://apps.facebook.com/537130286376086',
    to : cb.value,
    description : 'You have been bitch slapped by a friend',
    actions: [
        {'name': 'Slap Back', 'link': 'https://apps.facebook.com/537130286376086'}
        ],
}, function(response) {
});

FB.ui({
    method : 'feed',
    link: 'https://apps.facebook.com/537130286376086',
    description: "Someone just got bitch slapped",
    actions: [
        {'name': 'Slap Someone!', 'link': 'https://apps.facebook.com/537130286376086'}
        ],
}, function(response) {
});

}

All of this works just fine. The problem is when I try to bring actual data into the "posts". My first approach did not work and there was not an error, it simply did not produce a post.

description: "Someone just got bitch slapped" + cb.name,

As you can see, the var "cb" carries with it the data from the call. cb.value works just fine and it is called to in the same manner and what I am trying to do. But, rather than get too deep into that I quickly created a separate function that would pull the information I needed.

FB.api(cb.value, function(response) {

    var data1=response.name;
            return data1;


    }

I used alerts to check that both cb.value was coming through just fine and that response.name was correct and I checked spelling on all the data. I then pulled data1 back into the post and got "undefined" in the posting. I fiddled with this several ways and it just does not bring back data. The first approach made the most sense, but there is something seriously wrong here.

Upvotes: 1

Views: 180

Answers (2)

Dagan Bog-Computers
Dagan Bog-Computers

Reputation: 608

morantis, I have a built application and had some of these issues, I solved it by using FB.getLoginStatus with an access token, and when needed fb.ui inside these functions, it gave me full functionality within my app, and full control to all that data you are getting from user. also using global Objects, did the trick with keeping info like feeds.

Upvotes: 1

morantis
morantis

Reputation: 106

Using the original call, I solved half the problem. In the first function I changed the "name" from "friends" to response.data[i].name. That made it easier to pull that data from the first function. The fact is that the function was sending a "div" and all of its information.

Upvotes: 0

Related Questions