我将数组传递给函数,如果存在“facebookValue”则获取用户ID和访问令牌。
该函数正常工作,因为id可以输出到控制台...但我的问题似乎是返回值没有捕获变量,我得到未定义。
这是我的功能
function facebook_oAuth(arr){
var checkArray = $.inArray('facebookValue', arr),
uid,
accessToken;
if (checkArray == -1) {}
else{
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
uid = response.authResponse.userID,
access_token = response.authResponse.accessToken;
console.log(uid + " ? " + accessToken); // ok
}
return [uid, access_token];// doesnt return the values from above
});
}
};
我正在获取这样的值,但值(uid,accessToken)出现undefined
var myReturn = facebook_oAuth(myArray),
uid = myReturn[0],
accessToken = myReturn[1],
credentialsArray = [uid,accessToken];
答案 0 :(得分:1)
此代码是异步的,如果您确实需要获得facebook_oAuth
返回值的响应,则必须等待异步调用才能完成,但我不推荐。
Othrwise你可以使用这段代码:
function facebook_oAuth(arr,callback) {
var checkArray = $.inArray('facebookValue', arr),
uid,
accessToken;
if (checkArray == -1) {
}
else {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
uid = response.authResponse.userID,
access_token = response.authResponse.accessToken;
console.log(uid + " ? " + accessToken); // ok
}
callback([uid, access_token]);
});
}
}
;
facebook_oAuth(myArray,function(myReturn)
{
//This code is executed AFTER you retrieve FB login status
var uid = myReturn[0],
accessToken = myReturn[1],
credentialsArray = [uid,accessToken];
});