function getSenderName(senderId) {
var __name;
// Get user's first name from the User Profile API
// and include it in the greeting
request({
url: "https://graph.facebook.com/v2.6/" + senderId,
qs: {
access_token: process.env.PAGE_ACCESS_TOKEN,
fields: "first_name"
},
method: "GET"
}, function(error, response, body) {
if (error) {
console.log("Error getting user's name: " + error);
} else {
var bodyObj = JSON.parse(body);
__name = bodyObj.first_name;
console.log("1.SenderId : " + senderId + " Name : " + __name);
}
});
console.log("2.SenderId : " + senderId + " Name : " + __name);
return __name;
}
與上面的代碼中,我得到從函數的函數調用javascript中
1.SenderId : 1234 Name : Vinay
2.SenderId : 1234 Name : Undefined
任何人可以幫助我如何訪問變量,它是這樣的嵌套函數內部
這與嵌套函數無關 - 它與異步代碼有關 - 我**下注**您在1之前得到2個輸出 –
@JaromandaX是的!真的..我很新的JavaScript。有什麼辦法可以在 – vjain419