您好我想在我的网站上找到登录Facebook的人的家乡,并最终在谷歌地图上创建他们所在位置的标记。
我收到了Uncaught TypeError:无法读取属性' name'我的浏览器控制台中未定义的 var city = response.hometown.name; 。
请帮我解决这个问题,我已经尝试了之前发布的一些解决方案,但会根据建议再试一次。感谢。
以下是HTML中的facebook按钮:
<fb:login-button data-scope="public_profile,email,user_hometown" onlogin="checkLoginState();" autologoutlink="true">
</fb:login-button>
<div id="status"></div>
facebook.js文件:
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
console.log(JSON.stringify(response));
if (response.status == "connected") {
// send access token to server
authToServer(response, function() {
// after logged in
// get friends data and add to map
//addFriendsToMap();
})
};
})//, {scope: 'public_profile,email,user_hometown,user_location'});
}
window.fbAsyncInit = function() {
FB.init({
appId : '*****************',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.2', // use version 2.2
scope : 'public_profile,email,user_location,user_hometown'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
console.log(response);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
var city = response.hometown.name;
alert(city);
addFriendsToMap();
});
}
function authToServer(authResponse, callback) {
var authReq = new XMLHttpRequest();
authReq.onreadystatechange = function() {
if (authReq.readyState==4 && authReq.status==200) {
console.log("logged in with server");
callback();
}
}
authReq.open("POST", "http://localhost:3000/auth", true);
authReq.setRequestHeader("content-type", "application/json");
authReq.send(JSON.stringify(authResponse));
}
function addFriendsToMap() {
var dataURL = "http://localhost:3000/model/data.json";
var friendsReq = new XMLHttpRequest();
friendsReq.onreadystatechange = function() {
if (friendsReq.readyState==4 && friendsReq.status==200) {
// parse the response friendsReq.responseText as json
console.log("returned from server facebook request")
var jsonData = JSON.parse(friendsReq.responseText);
var friends = jsonData.friends;
var index = 0;
// for each element in the friends array add a marker
while (index < friends.length) {
var friend = friends[index];
var marker = new google.maps.Marker({
position: new google.maps.LatLng(
friend.lat,
friend.lng),
title: friend.name,
map: map
});
index++;
}
}
}
friendsReq.open("GET", dataURL, true);
friendsReq.send();
}