iamchriswick
iamchriswick

Reputation: 370

How to get data from an Facebook apprequest response?

I'm trying to figure out how I can get data from an apprequest response:

function invite(provider) {
    FB.ui({
        method: 'apprequests',
        message: 'Check this thing out!',
        title: 'Request Title'
    }, function(response) {
        if (response && !response.error) {
            console.log('3 friend invited.');
            console.log(response);
        } else {
            console.log('You have to invite 2 friends.');
            console.log(response);
        }
    });
}

console.log(response); returns:

Object {
    request: "667131760069164",
    to: Array[1]
}
request: "667131760069164"
to: Array[1] 0: "1476807679266627"
length: 1

I'm wondering how I can get the information stored in to: to check how many were invited.

Thoughts, anyone?

Upvotes: 0

Views: 294

Answers (2)

N Kumar
N Kumar

Reputation: 1312

Try this

obj = JSON.parse(response);
console.log(obj.to[0][0]);
console.log(obj.to[0].length);

You can access first value in 'to' array by obj.to[0][0] and obj.to[0].length

Upvotes: 1

iamchriswick
iamchriswick

Reputation: 370

I managed to get the result I wanted with:

if (response.to.length > 2) {
    console.log('Success! You have invited ' + response.to.length + ' friends.');
} else {
    console.log('You need to invite at least 3 friends')
}

Thanks for the suggestions.

Upvotes: 0

Related Questions