Anonymous Anonymous - 1 year ago 99
JSON Question

Javascript Uncaught TypeError: Facebook Like Counter

I have the following script to get the amount of likes from a Facebook page:

$("#facebook-likes").each(function () {
var container = $(this);
var fburl = container.attr('rel');
dataType: 'json',
type: 'GET',
url: '',
data: {
query: 'SELECT like_count FROM link_stat WHERE url="' + fburl + '"',
format: 'json'
}).success(function (data) {
container.text(data[0].like_count + ' Likes');

When running the script I get the following error:

Uncaught TypeError: Cannot read property 'like_count' of undefined

I've searched the internet for a solution but I can't find one solving this problem. I hope someone can help me out!

Answer Source

FQL is deprecated, this is the correct API to get the likes of a Page:[page-id]?fields=fan_count

You can test it in the API explorer:

Be aware that this needs an Access Token, so you should not do that client side with an App Access Token. You should also consider caching the result, not to hit API limits if there are a lot of users.

More information about Tokens:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download