javascript - Cannot read results from nodejs request result -
i make request towards fb graph api through node js' request:
request({ url: 'https://graph.facebook.com/v2.6/' + userid + '?fields=first_name,last_name&access_token=' + token, method: 'get', }, function (error, response, body) { console.log(error); }).on('response', function (response) { response.on('data', function (data) { console.log('user data ' + data); // logs user data { // "first_name": "marcus", // "last_name": "green" } var userdata = { firstname: data['first_name'], lastname: data['last_name'] }; console.log(userdata.firstname) // logs undefined }) });
same happens when assign data.first_name
or data.last_name
if first call console.log('user data ' + data);
returns:
"user data{"first_name":"a","last_name":"b"}"
and not:
"user data[object object]"
it means, data
object string, not object. if want use object , access it's fields, first parse using:
var parseddata = json.parse(data); console.log(parseddata.first_name); // logs first_name
Comments
Post a Comment