How to Access the fb Message Sender's User Profile Data outside Request() function code block?

Hi, I am new to Webhooks, API calls & request, and SDKs. I have been building Messenger Bots using ManyChat & FlowXO, but now I am required to create a Messenger bot via Facebook for Developers using Node.JS for the code. I have just finished the “Quick Start Tutorial”; and now, I am trying to personalize messages; but currently encountering problem as iterated below with the sample code.

*CODE written in NODE.JS deployed at Glitch.com

*a GET request for facebook’s Profile API using Express Http framework or (node module)

function ProfileName(senderID)
{
let JSbody;
request (
{ “uri”: “https://graph.facebook.com/v2.6/” + senderID,
“qs”: { “access_token”: process.env.PAGE_ACCESS_TOKEN,
“fields”: first_name , last_name , profile_pic },
“method”: “GET”,
},
function callback(err, res, body)
{ JSbody = JSON.parse(body);
console.log ( JSbody ); // < this will contain a json object with first_name , last_name & profile_pic data
);
console.log ( JSbody ); // < but this will contain a json object with an undefined value. WHY ?
return JSbody.first_name; // < and also this one will return an undefined value
}

*1) It seems that any value inside the ‘callback()’ function cant be used outside the ‘request()’ function even if you assigned it to a variable defined/initialized outside the request() function.

*2) can any one provide a solution on how to use the value of the “body” parameter from the ‘callback()’ function, outside the ‘request()’ function, such as returning the value of first_name as the return value of function ‘ProfileName()’.

*3) OR, is there an alternative for the ‘request()’ function in making API request & how, to avoid this problem I have?

Hi, welcome!

This is a problem with “callbacks” in general, and it’s to do with timing.

I’ll just re-post your code snippet to start with as the formatting got screwed up…

function ProfileName(senderID)
{
	let JSbody;
	request (
		{
			“uri”: “https://graph.facebook.com/v2.6/” + senderID,
			“qs”: { “access_token”: process.env.PAGE_ACCESS_TOKEN,
			“fields”: first_name , last_name , profile_pic },
			“method”: “GET”,
		},
		function callback(err, res, body)
		{
			JSbody = JSON.parse(body);
			console.log ( JSbody ); // < this will contain a json object with first_name , last_name & profile_pic data
		}
	);
	console.log ( JSbody ); // < but this will contain a json object with an undefined value. WHY ?
	return JSbody.first_name; // < and also this one will return an undefined value
}

Here’s the order of things happening:

  1. The request fires off
  2. You log the value of JSbody which is currently = undefined
  3. The ProfileName function returns undefined (or crashes)
  4. The response arrives and is handled by the callback function, which logs a real JSON object

Hopefully you see how the order of execution causes the problem. The callback only fires when the response comes back, but your function continues to execute before that.

Check out http://callbackhell.com/

So, what can you do about it? Here are some options:

1. Pass a callback function into ProfileName:

function ProfileName(senderID, cb)
{
	let JSbody;
	request (
		{
			“uri”: “https://graph.facebook.com/v2.6/” + senderID,
			“qs”: { “access_token”: process.env.PAGE_ACCESS_TOKEN,
			“fields”: first_name , last_name , profile_pic },
			“method”: “GET”,
		},
		cb
	);
}

ProfileName(senderID, function callback(err, res, body)
		{
			// Do something with body here to set profilename
		})

But honestly this may just move your problem up one layer instead of solving it.

2. Find a way to await the function

Some libraries let you use JavaScript await syntax instead of callbacks, which will pause execution until the API response comes back. Worth searching.

3. Promises

Promises are another JS alternative to callbacks, which some libraries may support. I can’t go into detail here but some online searches could help.

I wish I could be more help. Do check out http://callbackhell.com if you haven’t already.

Any more questions go ahead and ask :slight_smile:

3 Likes

Hi SteGriff, thank you very much for the Reply & Help. :slight_smile: I try all the options you gave & also visit the site you suggested, and give you feedback afterwards. I still got a long way to go being new to Webhooks & APIs.

About the Promises & Await: I have came across with it & thought it might be the solution to my problem but still hesitant with it, due to a lot of things I need to setup that I dont even yet understand & could still not solve the problem. But through your suggestions, Now I think it will. Hehe… :smile: Thanks again.

1 Like