1

I keep getting TypeError: Cannot read property 'first_name' of undefined when trying to execute this code (all variables, including modules are defined in other parts of the code):

if (messageText) {
            var senderID_first_name = f_User.UserQ(senderID);
            sendTextMessage(senderID, String(senderID_first_name));
        }

f_User = {
    UserQ: function(id) {
        var parse_body;
        request.get('https://graph.facebook.com/v2.6/'+id+'?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token='+PAGE_ACCESS_TOKEN, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                parse_body = body;
            }
        });
        return parse_body.first_name;
    }
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
Sarah Macey
  • 196
  • 1
  • 12

2 Answers2

1

You ran into a typical problem with the async behavior of JS. You returning date that you doesn't have yet. The data that you want are in the callback of the get method.

You could solve this in your architecture like this.

UserQ: function(id, callback) {
    var parse_body;
    request.get('https://graph.facebook.com/v2.6/'+id+'?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token='+PAGE_ACCESS_TOKEN, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            callback(parse_body.first_name);
        }
    });
}

and then

...
UserQ(2, function(name){
 console.log('Name for id 2', name);
});
...

You have to study about callbacks and promises in JS. Good luck :—)

P.S: See this example: http://jsbin.com/jagisumode/edit?js,console

jukben
  • 1,088
  • 7
  • 16
  • Thank you for the reply. I tried this and I keep getting undefined in return (at least it's not crashing!). `if (messageText) { f_User.UserQ(senderID, function(senderID_first_name) { sendTextMessage(senderID, String(senderID_first_name)); }); }` ` – Sarah Macey Jan 14 '17 at 10:06
  • `f_User = { UserQ: function(id, callback) { request.get('https://graph.facebook.com/v2.6/'+id+'?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token='+PAGE_ACCESS_TOKEN, function(error, response, body) { if (!error && response.statusCode == 200) { console.log(body.first_name); callback(body.first_name); } }); } }` – Sarah Macey Jan 14 '17 at 10:06
  • The `console.log(body.first_name); ` is undefined? What return `body` then? – jukben Jan 14 '17 at 10:16
  • `senderID_first_name` is returning undefined :( – Sarah Macey Jan 14 '17 at 10:17
  • See this: http://jsbin.com/jagisumode/edit?js,console How do you get that `senderID_first_name` is undefined? – jukben Jan 14 '17 at 10:24
0

I hope this will work for you, not tested on my end.

if (messageText) {
    f_User.UserQ(senderID,true);
}
f_User = {
    UserQ: function(id,sendMessage=false) {
        request.get('https://graph.facebook.com/v2.6/'+id+'?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token='+PAGE_ACCESS_TOKEN, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                if(sendMessage)              
                    sendTextMessage(id, String(body.first_name));
             }
        });
    }
}
Imran Qamer
  • 2,253
  • 3
  • 29
  • 52