0

I'm trying to get Facebook user profile info using the Graph API in nodejs where I am making the following request.

    var url = 'https://graph.facebook.com/v2.6/' + id
    var qs = {fields:'first_name,last_name',access_token:token}

    request({
        url: url,
        method: 'GET',
        qs
    }, function(err, response, body){
        var name = body.first_name
        var message = "Hey there " + name
    })
}

The request response from the server looks like this

{
  "first_name": "Peter",
  "last_name": "Chang"
}

However, the name is returned as undefined. But if I make the call like this.

    var url = 'https://graph.facebook.com/v2.6/' + id
    var qs = {fields:'first_name,last_name',access_token:token}

    request({
        url: url,
        method: 'GET',
        qs
    }, function(err, response, body){
        var name = body
        name = JSON.jsonstringify(body)
        var message = "Hey there " + name
    })
}

It gives an output like this,

   Hey there {"statusCode":200,"body":"{\"first_name\":\"Peter\",\"last_name\":\"Chang\"}"}

So why is the name is returned as undefined in the first example. What am I doing wrong? Please can someone explain. Thanks

0

3 Answers 3

1

I believe you have to tell request that you're dealing with a JSON response. You can use the json: true property in your request.

request({
  url: url,
  method: 'GET',
  qs,
  json: true
}, function (err, res, body) {
  var name = body.first_name
  var message = "Hey there " + name
});

From the request docs:

json - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

There is firstlevel body key also. So, name = body.body.first_name
If the server response only includes first_name/last_name then I think body.first_name will be fine. The reason it looks like there is body.body is because request is just returning the full response in the body variable (hence the status code etc)
1

I would say it is because the response has not been parsed and is therefor sill a string. Are you using nodes http module?

var url = 'https://graph.facebook.com/v2.6/' + id
var qs = {fields:'first_name,last_name',access_token:token}

request({
  url: url,
  method: 'GET',
  qs
}, function(err, response, body){
  var data = JSON.parse(body);
  var name = data.first_name
  var message = "Hey there " + name
});

1 Comment

Yes you are correct. Thanks for letting me know. It was my mistake. Your answer works. But I think @mrwillihog answer is more efficient. So I will accept that as the correct answer :)
0

You need to parse the body. To do that you can use the body-parser module

see

https://www.npmjs.com/package/body-parser#examples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.